2013-07-20 26 views
0

我在windows xp下創建圖標有問題。 這是我的代碼:在windows xp下創建位圖圖標

void SaveAsIcon(Bitmap SourceBitmap, string FilePath) 
{ 

      FileStream FS = new FileStream(FilePath, FileMode.Create); 
      // ICO header 
      FS.WriteByte(0); FS.WriteByte(0); 
      FS.WriteByte(1); FS.WriteByte(0); 
      FS.WriteByte(1); FS.WriteByte(0); 

      // Image size 
      FS.WriteByte((byte)SourceBitmap.Width); 
      FS.WriteByte((byte)SourceBitmap.Height); 
      // Palette 
      FS.WriteByte(0); 
      // Reserved 
      FS.WriteByte(0); 
      // Number of color planes 
      FS.WriteByte(0); FS.WriteByte(0); 
      // Bits per pixel 
      FS.WriteByte(32); FS.WriteByte(0); 

      // Data size, will be written after the data 
      FS.WriteByte(0); 
      FS.WriteByte(0); 
      FS.WriteByte(0); 
      FS.WriteByte(0); 

      // Offset to image data, fixed at 22 
      FS.WriteByte(22); 
      FS.WriteByte(0); 
      FS.WriteByte(0); 
      FS.WriteByte(0); 

      // Writing actual data 
      SourceBitmap.Save(FS, ImageFormat.Png); 

      // Getting data length (file length minus header) 
      long Len = FS.Length - 22; 

      // Write it in the correct place 
      FS.Seek(14, SeekOrigin.Begin); 
      FS.WriteByte((byte)Len); 
      FS.WriteByte((byte)(Len >> 8)); 

      FS.Close(); 
} 

它在Vista和更高版本下工作正常。

我不知道我應該改變我的方法來創建從Windows XP位圖圖標。

當我試着使用方法使用上面的圖標我得到這個錯誤:

錯誤生成Win32資源:讀取錯誤圖標

在Vista下,7,8它的工作原理。

+0

你從哪裏得到這個錯誤?我認爲圖標需要ico文件 – Sayse

+0

我在c#中動態編​​譯期間出現此錯誤。 CompilerParameters cp = new CompilerParameters(); cp.CompilerOptions =「/ optimize/warn:4/target:winexe /win32icon:icon.ico」; – Yozer

回答

1
SourceBitmap.Save(FS, ImageFormat.Png); 

XP不支持PNG格式的圖標,直到Vista纔會添加該功能。

如果XP支持很重要,您需要回退到BMP格式。請注意,只是更改ImageFormat是不夠的,您需要確保像素格式與您在標題中編寫的內容相匹配,並且不應寫入BITMAPFILEHEADER。換句話說,跳過Save()方法寫入的前14個字節。

+0

看看我的編輯。 – Yozer

+0

您使我的答案無效。這並不好,再次點擊提問問題按鈕。 –

+0

http://stackoverflow.com/questions/17763227/creating-icon-from-bitmap-strange-resizing – Yozer