Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to display Grayscale image after changing the indexing in C#
    text
    copied!<p>I have a Grayscale image which I am pulling from the DB (it is in Bytes). I want to draw some boxes using graphics object on it and then display this image. This is what was coded before -</p> <pre><code>public byte[] DrawOverlayOnGreyscaleImage(byte[] buffer, List&lt;ImagingTransaction.ImagingTransactionField&gt; TransactionFieldList, BLLImageType imageType) { //Load image into a bitmap object via first going into a MemoryStream MemoryStream msBitmap = new MemoryStream(buffer); Bitmap BitmapObj = null; BitmapObj = new Bitmap(msBitmap); int bmwidth = BitmapObj.Width; int bmheight = BitmapObj.Height; // draw some text on top Graphics g = Graphics.FromImage(BitmapObj); </code></pre> <p>Because of the changes in the way the image is now generated (Format8bppIndexed) the Graphics object threw an exception - "A graphics object cannot be created from an image that has an indexed pixel format". So I changed the Bitmap to be Format24bppRGB. Now, there is no exception. But after I draw boxes on the image and try to save it, the image is all black. This is because in case of "Grayscale" images R=G=B. This is lost after making it non indexed. I change the Bitmap to be again Indexed (Format8bbIndexed). Change the ColorPalette, but nothing helps. I still get the image to be totally black. Please help. My new code is as follows -</p> <pre><code>public byte[] DrawOverlayOnGreyscaleImage(byte[] buffer, List&lt;ImagingTransaction.ImagingTransactionField&gt; TransactionFieldList, BLLImageType imageType) { //Load image into a bitmap object via first going into a MemoryStream MemoryStream msBitmap = new MemoryStream(buffer); Bitmap BitmapObj = null; BitmapObj = new Bitmap(msBitmap); int bmwidth = BitmapObj.Width; int bmheight = BitmapObj.Height; Bitmap tmp = new Bitmap(BitmapObj.Width, BitmapObj.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(tmp); Rectangle srcRect; int RectWidth; int RectHeight; Pen myPen = new Pen(System.Drawing.Color.Red, 3); foreach (ImagingTransaction.ImagingTransactionField Field in TransactionFieldList) { // first, do they want to see the rectangles if (imageType == BLLImageType.GreyScale_With_FieldRectangles || imageType == BLLImageType.GreyScale_With_FieldRectangles_And_Field_Data) { RectWidth = Field.LowerRightX - Field.UpperLeftX; RectHeight = Field.LowerRightY - Field.UpperLeftY; // sanity check for negative values if (RectWidth &lt;= 0) RectWidth = 10; if (RectHeight &lt;= 0) RectHeight = 10; srcRect = new Rectangle(Field.UpperLeftX, Field.UpperLeftY, RectWidth, RectHeight); g.DrawRectangle(myPen, srcRect); } // now, do they want to see the text to the lower right of the field if (imageType == BLLImageType.GreyScale_With_Field_Data || imageType == BLLImageType.GreyScale_With_FieldRectangles_And_Field_Data) { g.DrawString(Field.FieldValue, new Font("Tahoma", 12), Brushes.Red, new PointF(Field.LowerRightX, Field.LowerRightY)); ; } } MemoryStream msBitmapWithRectangle = new MemoryStream(); // Save to memory using the Jpeg format Bitmap tmp2 = new Bitmap(tmp.Width, tmp.Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed); ColorPalette pal = tmp2.Palette; for (int i = 0; i &lt; pal.Entries.Length; i++) { // create greyscale color table pal.Entries[i] = Color.FromArgb(i, i, i); } tmp2.Palette = pal; tmp2.Save(msBitmapWithRectangle, System.Drawing.Imaging.ImageFormat.Jpeg); // read to end byte[] ByteArrayWithRectangle = msBitmapWithRectangle.GetBuffer(); // cleanup tmp.Dispose(); tmp2.Dispose(); BitmapObj.Dispose(); msBitmap.Close(); msBitmapWithRectangle.Close(); return ByteArrayWithRectangle; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload