3

如何从图形对象创建位图对象?我想从我的 Graphics 对象中读取像素。例如,像 System.Drawing.BitMap.GetPixel()。

我正在尝试找出 pdf 文档中的空白区域(全白或任何颜色),以编写一些图形/图像。我试过这样,但它不起作用。为什么下面的代码没有按预期工作?

//
// System.Drawing.Bitmap
// System.Drawing.Graphics
//
Bitmap b = new Bitmap(width, height, graphics);

//
// In this case, for any (i, j) values, Bitmap.GetPixel returns 0
//
int rgb = b.GetPixel(i, j).ToArgb();

(在 .net-only 上下文中再次发布此问题,删除其他库依赖项)

4

3 回答 3

0

(很晚了,但是……)

你有没有尝试过

var bmp = System.Drawing.Bitmap.FromHbitmap(gr.GetHdc());

然后你可以从bmp.

于 2014-06-25T21:41:41.263 回答
0

理想情况下,您希望避免 GetPixel/SetPixel 并使用对位图的不安全访问方法来提高速度。

System.Drawing.Graphics 图形 = System.Drawing.Graphics.FromImage(bitmap);

然后使用图形实例。如果我记得,修改图形对象会修改位图。

于 2010-06-11T11:51:25.620 回答
0

首先,您应该创建位图,然后从该位图创建图形,使用图形,然后保存(或使用它)位图。

您的代码将如下所示:

using (Bitmap image = new Bitmap(X, Y))
{
    using (Graphics gr = Graphics.FromImage(image))
    {
        // work with graphics, Draw objects
    }
    image.Save("YourPathToFile"); // Or GetPixel, if you want
}

您的代码无法正常工作,因为 Bitmap 的构造函数仅获取 Graphics 以进行解析。MSDN告诉Initializes a new instance of the Bitmap class with the specified size and with the resolution of the specified Graphics object.

于 2010-06-11T12:33:02.250 回答