How to create a Bitmap object from a Graphics object?

How to create a Bitmap object from a Graphics object? I would like to read pixels from my Graphics object. e.g. System.Drawing.BitMap.GetPixel ().

I am trying to find an empty area (all white or any color) inside a pdf document to write some graphics / image. I tried so, but it does not work. why is the following code not working as expected?

//
// 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();

(re-asking this question in the context of .net-only, removing other library dependencies)

+3
source share
3 answers

Ideally, you want to avoid GetPixel / SetPixel and use unsafe bitmap access methods for some speed.

System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);

. , .

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.

0

( , ...)

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

bmp.

0

Source: https://habr.com/ru/post/1749556/


All Articles