2

I've got a bitmap that's 827852 pixels wide by 1 pixel high in a Bitmap object. When converting the bitmap to a BitmapSource for rendering in WPF, the following artifacts appear in the UI: An image with a rendering artifact

The black portion in the middle is not in the original bitmap, and the yellow parts should be green, something like this: A proper, but zoomed in rendering without artifacts

By plain old trial and error I've determined that the problem appears when the original bitmap is wider than 524287 pixels. If it's 524288 or larger, I get rendering artifacts after converting it to a BitmapSource. 524287 is 0x7FFFF, which seems to indicate a overflow, but I'm unsure of where.

I'm using the following conversion method:

public BitmapSource Convert(Bitmap bitmap)
{
    var bitmapData = bitmap.LockBits(
        new Rectangle(0, 0, bitmap.Width, bitmap.Height),
        System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

    var bitmapSource = BitmapSource.Create(
        bitmapData.Width, bitmapData.Height, 96, 96, PixelFormats.Bgr24, null,
        bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, bitmapData.Stride);

    bitmap.UnlockBits(bitmapData);

    return bitmapSource;
}

taken from fast converting Bitmap to BitmapSource wpf

Any ideas where the issue occurs and how to work around it? I'm leaning towards rescaling the bitmap to be <524288 pixels manually.

Community
  • 1
  • 1
Nattfrosten
  • 1,999
  • 4
  • 16
  • 21
  • Did you try any of the other conversion methods, e.g. CreateBitmapSourceFromHBitmap? – Clemens Jan 31 '17 at 10:06
  • According to the Remarks section on the [BitmapSource](https://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapsource(v=vs.110).aspx) page, the "maximum height and width of an image is 2^16 pixels at 32 bits per channel ..." – Clemens Jan 31 '17 at 10:13
  • @Clemens Yes, I did, got the same result. Changing the formats to System.Drawing.Imaging.PixelFormat.Format32bppRgb/ PixelFormats.Bgra32 only serves to make the black part transparent. – Nattfrosten Jan 31 '17 at 10:22

0 Answers0