2011-03-22 153 views
0

從數組中創建位圖有幾個問題。我有一個相機,從這裏我得到了格式化的灰度值。但如何從這個值創建一個位圖?只有:從ushort陣列創建.bmp

System.Drawing.Bitmap checks = new System.Drawing.Bitmap(10, 10); 
. 
. 
checks.Save(@"C:\test.bmp", ImageFormat.Bmp); 

不會工作:(我得到的圖像,並可以與窗口的工具打開它,但是當我將打開該文件與另一個圖形lib中,我得到了很多錯誤,所以現在如何做任何人。創建具有頭部等正確的bmp文件?沒有任何人有一些代碼例子嗎?這將有助於最。

感謝

+0

您有關於如何解釋樣本的規範? – driis 2011-03-22 21:43:36

+0

我現在的分辨率(例如160 x 160),我現在灰色值在16位。所以65526是最高的值。這將被解釋爲過度曝光。 – h0ppel 2011-03-22 21:48:20

回答

2

您應該創建正確的尺寸(寬,高)Bitmap,並使用LockBits來得到一個你應該寫入的內存句柄如果你的數據是在.NET支持的PixelFormat中,你可以將它傳遞給LockBits並且簡化y複製數據。如果不是,您可能需要手動進行一些數據轉換。

這一切都歸結爲您接收數據樣本的格式,但上面的描述概述了您需要採取的步驟來生成圖像。

更新:因爲你的數據是16位灰度,有一個PixelFormat你可以直接使用,PixelFormat.16bppGrayScale

0
public class(path,wid,height,boolean) 
{ 

System.Drawing.Image myThumbnail150; 
      System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); 
      System.Drawing.Image imagesize = System.Drawing.Image.FromFile(pic.FilePath); 
      using (Bitmap bitmapNew = new Bitmap(imagesize)) 
      { 

      double maxWidth = Convert.ToDouble(ConfigurationSettings.AppSettings["ImageWidth"]); 
      double maxHeight = Convert.ToDouble(ConfigurationSettings.AppSettings["ImageHeight"]); 
      int w = imagesize.Width; 
      int h = imagesize.Height; 
      // Longest and shortest dimension 
      int longestDimension = (w > h) ? w : h; 
      int shortestDimension = (w < h) ? w : h; 
      // propotionality 
      float factor = ((float)longestDimension)/shortestDimension; 
      // default width is greater than height  
      double newWidth = maxWidth; 
      double newHeight = maxWidth/factor; 
      // if height greater than width recalculate 
      if (w < h) 
      { 
       newWidth = maxHeight/factor; 
       newHeight = maxHeight; 
      } 
      myThumbnail150 = bitmapNew.GetThumbnailImage((int)newWidth, (int)newHeight, myCallback, IntPtr.Zero); 
      string name = pic.Name.Replace(Path.GetExtension(pic.Name), ".Bmp"); 

      //Create a new directory name ThumbnailImage 
      //Save image in TumbnailImage folder 
      myThumbnail150.Save(yourpath+ name, System.Drawing.Imaging.ImageFormat.Bmp); 
      bitmapNew.Dispose(); 
} 
+0

好吧謝謝,但我不知道如何爲像素設置正確的顏色。當我有如下灰度值時:23455,58877,並設置像素顏色:checks.SetPixel(cols,rows,color);我是否需要轉換值?如何? thsnk – h0ppel 2011-03-23 18:37:28

+0

[遵循本教程](http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale)[或這一個](http:// www。 bobpowell.net/grayscale.htm)這解釋了不同的可能性。 – safi 2011-03-24 08:30:41