2013-07-17 46 views
0

我試圖將位圖保存爲ico文件格式,但結果文件是擴展名更改爲「ico」的PNG圖像。將位圖保存爲ICO文件,具有透明度

爲什麼ImageFormat類將我的位圖保存爲PNG格式,如果我選擇圖標格式?然後如何保存ico文件?

PS:我需要保存ICO透明度

這是我如何調用PROC:

Save_Icon(Resize_Image(Bitmap.FromFile(PictureBox_Regedit.Tag), 24, 24), "Regedit.ico") 

這是代碼

Private Sub Save_Icon(ByVal Source As Bitmap, ByVal Filename As String) 

    Try 
     If Not Directory.Exists(AppDir) Then Directory.CreateDirectory(AppDir) 
     If Not Directory.Exists(AppIcons) Then Directory.CreateDirectory(AppIcons) 
     Source.MakeTransparent() 
     Source.Save(Path.Combine(AppIcons, Filename), ImageFormat.Icon) 
    Catch ex As Exception 
     Throw New Exception(ex.Message) 
    End Try 

End Sub 

Private Function Resize_Image(ByVal img As Image, ByVal Width As Int32, ByVal Height As Int32) As Bitmap 
    Dim Bitmap_Source As New Bitmap(img) 
    Dim Bitmap_Dest As New Bitmap(CInt(Width), CInt(Height)) 
    Dim Graphic As Graphics = Graphics.FromImage(Bitmap_Dest) 
    Graphic.DrawImage(Bitmap_Source, 0, 0, Bitmap_Dest.Width + 1, Bitmap_Dest.Height + 1) 
    Return Bitmap_Dest 
End Function 

回答

1

編輯應用程序中的「另存爲」總是意味着轉換,這是您在此處未執行的操作。

例如,您可以使用Bitmap.GetHicon Method將源代碼轉換爲圖標格式,然後使用System.Drawing Icon Class。之後不要忘記摧毀Hicon

問題是,如果我沒有記錯,那就是.NET Framework離開你的地方。我無法回想起透明度功能,幾年前我已經切換到FreeImage圖書館,並且從那時起就做了所有事情。

+1

經過在谷歌大量研究有關如何在.NET中使用lib你是對的FreeImage是最好的方式嘿嘿,謝謝 – ElektroStudios

+0

只有一件事,我不能打開並編譯針對64位proyects(anycpu或x64配置)的源代碼,也許請你可以鏈接已編譯的freeimage dll for anycpu和x64,如下所示?:http://www.sambeauvois.be/blog/2010/05/freeimage-and-x64-projects-yes-you-can/ – ElektroStudios

+0

我自己編譯了一個64位版本,但我添加並更改了一些內容,它是2010版本。 – Alexander

2

的水庫由於它說in the documentation

當你調用MakeTransparent,位圖將轉換爲格式32bppArgb格式的 ,因爲此格式支持Alpha通道。

圖標不支持alpha通道。圖標透明度以不同的方式實現。 Save方法盡最大努力保持您創建的結構完整,因此也是PNG的結果。

+0

感謝您的回答,但即使沒有使用「maketransparent」,結果也是一個PNG文件。我不知道其他方式來保存ICO ... – ElektroStudios

相關問題