2011-09-02 53 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和您需要的大小中創建一個新的位圖,然後繪製圖像,然後繪製矩形。