2011-09-02 56 views
1

我有一个灰度图像,我从数据库(它是在字节中)拉。我想绘制一些使用图形对象的盒子,然后显示这个图像。这是在编码之前 -如何在C#中更改索引后显示灰度图像

public byte[] DrawOverlayOnGreyscaleImage(byte[] buffer, List<ImagingTransaction.ImagingTransactionField> 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); 

因为现在产生的图像的方式的变化(Format8bppIndexed)的Graphics对象抛出异常 - “一个图形对象不能从具有的索引图像创建像素格式“。所以我将位图改为Format24bppRGB。现在,没有例外。但是当我在图像上画框并尝试保存时,图像全是黑色的。这是因为在“灰度”图像R = G = B的情况下。在将其设为非索引之后,这会丢失。我将位图更改为Indexed(Format8bbIndexed)。更改ColorPalette,但没有任何帮助。我仍然觉得图像完全是黑色的。请帮忙。我的新代码如下 -

public byte[] DrawOverlayOnGreyscaleImage(byte[] buffer, List<ImagingTransaction.ImagingTransactionField> 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 <= 0) 
       RectWidth = 10; 
      if (RectHeight <= 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 < 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; 
} 
+1

对不起,也许我错过了一些东西,但它似乎创建了tmp2,但从未填充原始位图。在实践中,你可以在内存中保存完美的黑色矩形。 – Tigran

+0

非常感谢。不知道我错过了什么。 – sdd

+0

我会添加如此喜欢的答案。 – Tigran

回答

1

似乎TMP2创建,但从来没有充满原始位,所以你创建一个完美的黑色矩形。

0

尝试在BPP和您需要的大小中创建一个新的位图,然后绘制图像,然后绘制矩形。