Bug 27377 - Bitmap interprets 24-bit pixel data as 32-bit
Summary: Bitmap interprets 24-bit pixel data as 32-bit
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Drawing ()
Version: 3.12.0
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-02-24 14:49 UTC by michael.bennett
Modified: 2015-02-24 14:52 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Corrupted example (43.95 KB, image/jpeg)
2015-02-24 14:49 UTC, michael.bennett
Details
Correct example (19.29 KB, image/jpeg)
2015-02-24 14:52 UTC, michael.bennett
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 27377 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
NEW

Description michael.bennett 2015-02-24 14:49:15 UTC
Created attachment 10008 [details]
Corrupted example

When I create a Bitmap from raw pixel data using PixelFormat.Format24bppRgb, the data is interpreted as if it is PixelFormat.Format32bppRgb.

Example (produces corrupted image):

private static void SaveThumbnail(byte[] rgbBytes, string thumbnailFilePath, Size thumbnailSize)
{
	int stride = thumbnailSize.Width * 3;

	var pinnedArray = GCHandle.Alloc(rgbBytes, GCHandleType.Pinned);
	try
	{
		IntPtr ptr = pinnedArray.AddrOfPinnedObject();
		using (var source = new Bitmap(thumbnailSize.Width, thumbnailSize.Height, stride, PixelFormat.Format24bppRgb, ptr))
		{
			source.Save(thumbnailFilePath, ImageFormat.Jpeg);
		}
	}
	finally
	{
		pinnedArray.Free();
	}
}

If I manually reformat the pixels as 32bpp, and use PixelFormat.Format32bppRgb, the data is interpreted correctly.

Example (produces correct image):

private static void SaveThumbnail(byte[] rgbBytes, string thumbnailFilePath, Size thumbnailSize)
{
	int stride = thumbnailSize.Width * 3;
	stride = Convert24bppRgbTo32bppRgb(thumbnailSize, stride, ref rgbBytes);

	var pinnedArray = GCHandle.Alloc(rgbBytes, GCHandleType.Pinned);
	try
	{
		IntPtr ptr = pinnedArray.AddrOfPinnedObject();
		using (var source = new Bitmap(thumbnailSize.Width, thumbnailSize.Height, stride, PixelFormat.Format32bppRgb, ptr))
		{
			source.Save(thumbnailFilePath, ImageFormat.Jpeg);
		}
	}
	finally
	{
		pinnedArray.Free();
	}
}

private static int Convert24bppRgbTo32bppRgb(Size frameSize, int stride, ref byte[] rgbBytes)
{
	int newStride = frameSize.Width*4;
	byte[] newBytes = new byte[newStride*frameSize.Height];

	for (int y = 0; y < frameSize.Height; y++)
	{
		for (int x = 0; x < frameSize.Width; x++)
		{
			int oldIndex = y*stride + 3*x;
			int newIndex = y*newStride + 4*x;

			newBytes[newIndex] = rgbBytes[oldIndex];
			newBytes[newIndex + 1] = rgbBytes[oldIndex + 1];
			newBytes[newIndex + 2] = rgbBytes[oldIndex + 2];
			newBytes[newIndex + 3] = 0;
		}
	}

	rgbBytes = newBytes;

	return newStride;
}
Comment 1 michael.bennett 2015-02-24 14:52:59 UTC
Created attachment 10009 [details]
Correct example