Using WritePixels when using a writeable bitmap fr

2019-05-30 11:32发布

Im currently trying to use writeablebitmap to take a IntPtr of a scan of images and turn each one into a Bitmap. Im wanting to use writeablebitmap because im having an issue with standard gdi GDI+ System.Drawing.Bitmap gives error Parameter is not valid intermittently

There is a method on a WriteableBitmap that called WritePixels http://msdn.microsoft.com/en-us/library/aa346817.aspx

Im not sure what I set for the buffer and the stride every example I find it shows the stride as 0 although that throws an error. When I set the stride to 5 the image appear black. I know this may not be the most efficient code but any help would be appreciated.

//create bitmap header
bmi = new BITMAPINFOHEADER();

//create initial rectangle
Int32Rect rect = new Int32Rect(0, 0, 0, 0);

//create duplicate intptr to use while in global lock
dibhand = dibhandp;
bmpptr = GlobalLock(dibhand);

//get the pixel sizes
pixptr = GetPixelInfo(bmpptr);

//create writeable bitmap
var wbitm = new WriteableBitmap(bmprect.Width, bmprect.Height, 96.0, 96.0, System.Windows.Media.PixelFormats.Bgr32, null);

//draw the image
wbitm.WritePixels(rect, dibhandp, 10, 0);

//convert the writeable bitmap to bitmap
var stream = new MemoryStream();

var encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(wbitm));
encoder.Save(stream);

byte[] buffer = stream.GetBuffer();
var bitmap = new System.Drawing.Bitmap(new MemoryStream(buffer));

GlobalUnlock(dibhand);
GlobalFree(dibhand);
GlobalFree(dibhandp);
GlobalFree(bmpptr);
dibhand = IntPtr.Zero;

return bitmap;

1条回答
家丑人穷心不美
2楼-- · 2019-05-30 12:16

An efficient way to work on Bitmaps in C# is to pass temporarily in unsafe mode (I know I don't answer the question exactly but I think the OP did not manage to use Bitmap, so this could be a solution anyway). You just have to lock bits and you're done:

unsafe private void GaussianFilter()
{
    // Working images
    using (Bitmap newImage = new Bitmap(width, height))
    {
        // Lock bits for performance reason
        BitmapData newImageData = newImage.LockBits(new Rectangle(0, 0, newImage.Width,
            newImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

        byte* pointer = (byte*)newImageData.Scan0;
        int offset = newImageData.Stride - newImageData.Width * 4;

        // Compute gaussian filter on temp image
        for (int j = 0; j < InputData.Height - 1; ++j)
        {
            for (int 0 = 1; i < InputData.Width - 1; ++i)
            {
                // You browse 4 bytes per 4 bytes
                // The 4 bytes are: B G R A
                byte blue = pointer[0];
                byte green = pointer[1];
                byte red = pointer[2];
                byte alpha = pointer[3];

                // Your business here by setting pointer[i] = ...
                // If you don't use alpha don't forget to set it to 255 else your whole image will be black !!

                // Go to next pixels
                pointer += 4;
            }
            // Go to next line: do not forget pixel at last and first column
            pointer += offset;
        }


        // Unlock image
        newImage.UnlockBits(newImageData);
        newImage.Save("D:\temp\OCR_gray_gaussian.tif");
    }
}

This is really much more efficient than SetPixel(i, j), you just have to be careful about pointer limits (and not forget to unlock data when you're done).

Now to answer your question about stride: the stride is the length in bytes of a line, it is a multiple of 4. In my exemple I use the format Format32bppArgb which uses 4 bytes per pixel (R, G, B and alpha), so newImageData.Stride and newImageData.Width * 4 are always the same. I use the offset in my loops only to show where it would be necessary.

But if you use another format, for instance Format24bppRgb which uses 3 bytes per pixel (R, G and B only), then there may be an offset between stride and width. For an image 10 * 10 pixels in this format, you will have a stride of 10 * 3 = 30, + 2 to reach nearest multiple of 4, i.e. 32.

查看更多
登录 后发表回答