Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# FreeImage中的透明度变得疯狂_C#_.net_Vb.net_Png_Freeimage - Fatal编程技术网

C# FreeImage中的透明度变得疯狂

C# FreeImage中的透明度变得疯狂,c#,.net,vb.net,png,freeimage,C#,.net,Vb.net,Png,Freeimage,在我的程序中,我想加载PNG或ICO文件(具有透明度),然后将其另存为具有透明度的ICO文件 这是用于测试的随机png图像: …正如你所看到的,它是透明的 但当我尝试将其转换为ICO时,结果是: (图片取自Windows资源管理器) 所有的黑色都消失了 我尝试过使用专业软件如photoshop或ToyCon进行同样的转换,结果ICO文件没有这个问题 这是我的代码,是我第一次使用FreeImage,我对图像和alpha通道或透明度不太了解: Imports FreeImageAPI 'Dim

在我的程序中,我想加载PNG或ICO文件(具有透明度),然后将其另存为具有透明度的ICO文件

这是用于测试的随机png图像:

…正如你所看到的,它是透明的

但当我尝试将其转换为ICO时,结果是:

(图片取自Windows资源管理器)

所有的黑色都消失了

我尝试过使用专业软件如photoshop或ToyCon进行同样的转换,结果ICO文件没有这个问题

这是我的代码,是我第一次使用FreeImage,我对图像和alpha通道或透明度不太了解:

Imports FreeImageAPI

'Dim img As FreeImageAPI.FIBITMAP = FreeImage.Load(format, file, FREE_IMAGE_LOAD_FLAGS.ICO_MAKEALPHA)

Public Class Form1

Dim file As String = "C:\Users\Administrador.ELEKTRO-PC\Desktop\auricular 5.png"

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Shown

    Dim format As FreeImageAPI.FREE_IMAGE_FORMAT = FreeImage.GetFileType(file, 32)

    Dim img As FreeImageAPI.FIBITMAP = FreeImage.LoadEx(file)

    Dim Transparency As Byte() = New Byte(0) {}

    FreeImage.SetTransparencyTable(img, Transparency)

    FreeImage.Save(FREE_IMAGE_FORMAT.FIF_ICO, img, "c:\test.ico", FREE_IMAGE_SAVE_FLAGS.DEFAULT)

End Sub

End Class
完成了

我已经删除了透明度表,看起来一切正常,最后不需要设置透明度,我还创建了一个新手助手类来轻松管理库的一些基本功能(转换图像、旋转、灰度、调整大小…):

#Region " FreeImage Helper "

' [ FreeImage Helper ]
'
' // By Elektro H@cker
'
'
' INSTRUCTIONS:
' 1. ADD A REFERENCE FOR "FreeImageNET.dll" IN THE PROJECT.
' 2. ADD THE "FREEIMAGE.DLL" IN THE PROJECT.
'
'
' Examples :
'
' MsgBox(FreeImageHelper.Is_Avaliable() ' Result: True
' MsgBox(FreeImageHelper.Get_Version()  ' Result: 3.15.1
' MsgBox(FreeImageHelper.Get_ImageFormat("C:\Test.png")) ' Result: PNG
'
' FreeImageHelper.Convert("C:\Test.png", "C:\Test.ico", FreeImageAPI.FREE_IMAGE_FORMAT.FIF_ICO)
' FreeImageHelper.Convert(New Bitmap("C:\Test.png"), "C:\Test.jpg", FreeImageAPI.FREE_IMAGE_FORMAT.FIF_JPEG, FreeImageAPI.FREE_IMAGE_SAVE_FLAGS.JPEG_SUBSAMPLING_444 Or FreeImageAPI.FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYSUPERB)
'
' PictureBox1.BackgroundImage = FreeImageHelper.GrayScale(New Bitmap("C:\Test.bmp"))
' PictureBox1.BackgroundImage = FreeImageHelper.GrayScale("C:\Test.bmp")
'
' PictureBox1.BackgroundImage = FreeImageHelper.Resize(New Bitmap("C:\Test.bmp"), 32, 32)
' PictureBox1.BackgroundImage = FreeImageHelper.Resize("C:\Test.bmp", 64, 128)
'
' PictureBox1.BackgroundImage = FreeImageHelper.Rotate(New Bitmap("C:\Test.bmp"), 90)
' PictureBox1.BackgroundImage = FreeImageHelper.Rotate("C:\Test.bmp", -90)
'
' PictureBox1.BackgroundImage = FreeImageHelper.Thumbnail(New Bitmap("C:\Test.png"), 64, True)
' PictureBox1.BackgroundImage = FreeImageHelper.Thumbnail("C:\Test.png", 64, True)



Imports FreeImageAPI

Public Class FreeImageHelper

' <summary>
' Checks if <i>FreeImage.dll</i> is avaliable on the system.
' </summary>
Public Shared Function Is_Avaliable() As Boolean
    Return FreeImage.IsAvailable
End Function

' <summary>
' Gets the version of FreeImage.dll.
' </summary>
Shared Function Get_Version() As String
    Return FreeImage.GetVersion
End Function

' <summary>
' Gets the image format of a image file.
' </summary>
Shared Function Get_ImageFormat(ByVal File As String) As String
    Return FreeImage.GetFileType(File, 0).ToString.Substring(4)
End Function

' <summary>
' Convert a Bitmap object between image formats and save it to disk.
' </summary>
Shared Sub Convert(ByVal bmp As System.Drawing.Bitmap, _
                   ByVal Output As String, _
                   ByVal NewFormat As FREE_IMAGE_FORMAT, _
                   Optional ByVal SaveFlags As FREE_IMAGE_SAVE_FLAGS = FREE_IMAGE_SAVE_FLAGS.DEFAULT)

    Try
        FreeImage.SaveBitmap(bmp, Output, NewFormat, SaveFlags)
    Catch ex As Exception
        ' Throw New Exception(ex.Message)
        MsgBox(ex.Message)
    End Try

End Sub

' <summary>
' Convert a image file between image formats and save it to disk.
' </summary>
Shared Sub Convert(ByVal File As String, _
                   ByVal Output As String, _
                   ByVal NewFormat As FREE_IMAGE_FORMAT, _
                   Optional ByVal SaveFlags As FREE_IMAGE_SAVE_FLAGS = FREE_IMAGE_SAVE_FLAGS.DEFAULT)

    Try
        FreeImage.Save(NewFormat, FreeImage.LoadEx(File), Output, SaveFlags)
    Catch ex As Exception
        ' Throw New Exception(ex.Message)
        MsgBox(ex.Message)
    End Try

End Sub

' <summary>
' GrayScales a Bitmap object.
' </summary>
Shared Function GrayScale(ByVal bmp As System.Drawing.Bitmap) As System.Drawing.Bitmap

    Try

        Dim ImageStream As New System.IO.MemoryStream
        bmp.Save(ImageStream, bmp.RawFormat)

        Dim Image As FIBITMAP = FreeImage.LoadFromStream(ImageStream)
        ImageStream.Dispose()

        Return FreeImage.GetBitmap(FreeImage.ConvertToGreyscale(Image))

    Catch ex As Exception
        ' Throw New Exception(ex.Message)
        MsgBox(ex.Message)
        Return Nothing
    End Try

End Function

' <summary>
' GrayScales a image file.
' </summary>
Shared Function GrayScale(ByVal File As String) As System.Drawing.Bitmap

    Try
        Return FreeImage.GetBitmap(FreeImage.ConvertToGreyscale(FreeImage.LoadEx(File)))
    Catch ex As Exception
        ' Throw New Exception(ex.Message)
        MsgBox(ex.Message)
        Return Nothing
    End Try

End Function

' <summary>
' Resizes a Bitmap object.
' </summary>
Shared Function Resize(ByVal bmp As System.Drawing.Bitmap, _
                       ByVal X As Int32, _
                       ByVal Y As Int32, _
                       Optional ByVal Quality As FREE_IMAGE_FILTER = FREE_IMAGE_FILTER.FILTER_BILINEAR) As System.Drawing.Bitmap

    Try

        Dim ImageStream As New System.IO.MemoryStream
        bmp.Save(ImageStream, bmp.RawFormat)

        Dim Image As FIBITMAP = FreeImage.LoadFromStream(ImageStream)
        ImageStream.Dispose()

        Return FreeImage.GetBitmap(FreeImage.Rescale(Image, X, Y, Quality))

    Catch ex As Exception
        ' Throw New Exception(ex.Message)
        MsgBox(ex.Message)
        Return Nothing
    End Try

End Function

' <summary>
' Resizes a image file.
' </summary>
Shared Function Resize(ByVal File As String, _
                       ByVal X As Int32, _
                       ByVal Y As Int32, _
                       Optional ByVal Quality As FREE_IMAGE_FILTER = FREE_IMAGE_FILTER.FILTER_BILINEAR) As System.Drawing.Bitmap

    Try

        Return FreeImage.GetBitmap(FreeImage.Rescale(FreeImage.LoadEx(File), X, Y, Quality))

    Catch ex As Exception
        ' Throw New Exception(ex.Message)
        MsgBox(ex.Message)
        Return Nothing
    End Try

End Function

' <summary>
' Rotates a Bitmap object.
' </summary>
Shared Function Rotate(ByVal bmp As System.Drawing.Bitmap, _
                       ByVal Angle As Double) As System.Drawing.Bitmap

    Try

        Dim ImageStream As New System.IO.MemoryStream
        bmp.Save(ImageStream, bmp.RawFormat)

        Dim Image As FIBITMAP = FreeImage.LoadFromStream(ImageStream)
        ImageStream.Dispose()

        Return FreeImage.GetBitmap(FreeImage.Rotate(Image, Angle))

    Catch ex As Exception
        ' Throw New Exception(ex.Message)
        MsgBox(ex.Message)
        Return Nothing
    End Try

End Function

' <summary>
' Rotates a image file.
' </summary>
Shared Function Rotate(ByVal File As String, _
                       ByVal Angle As Double) As System.Drawing.Bitmap

    Try

        Return FreeImage.GetBitmap(FreeImage.Rotate(FreeImage.LoadEx(File), Angle))

    Catch ex As Exception
        ' Throw New Exception(ex.Message)
        MsgBox(ex.Message)
        Return Nothing
    End Try

End Function

' <summary>
' Returns a Thumbnail of a Bitmap object.
' </summary>
Shared Function Thumbnail(ByVal bmp As System.Drawing.Bitmap, _
                               ByVal size As Int32, _
                               ByVal convert As Boolean) As System.Drawing.Bitmap

    Try

        Dim ImageStream As New System.IO.MemoryStream
        bmp.Save(ImageStream, bmp.RawFormat)

        Dim Image As FIBITMAP = FreeImage.LoadFromStream(ImageStream)
        ImageStream.Dispose()

        Return FreeImage.GetBitmap(FreeImage.MakeThumbnail(Image, size, convert))

    Catch ex As Exception
        ' Throw New Exception(ex.Message)
        MsgBox(ex.Message)
        Return Nothing
    End Try

End Function

' <summary>
' Returns a Thumbnail of a image file.
' </summary>
Shared Function Thumbnail(ByVal File As String, _
                               ByVal size As Int32, _
                               ByVal convert As Boolean) As System.Drawing.Bitmap

    Try
        Return FreeImage.GetBitmap(FreeImage.MakeThumbnail(FreeImage.LoadEx(File), size, convert))
    Catch ex As Exception
        ' Throw New Exception(ex.Message)
        MsgBox(ex.Message)
        Return Nothing
    End Try

End Function

End Class

#End Region
#区域“FreeImage助手”
“[FreeImage帮助程序]
'
“//作者:ElektroH@cker
'
'
“说明:
' 1. 在项目中添加对“FreeImageNET.dll”的引用。
' 2. 在项目中添加“FREEIMAGE.DLL”。
'
'
例如:
'
'MsgBox(FreeImageHelper.Is_Available()'结果:True
'MsgBox(FreeImageHelper.Get_Version()'结果:3.15.1
“MsgBox(FreeImageHelper.Get_ImageFormat(“C:\Test.png”))”结果:png
'
'FreeImageHelper.Convert(“C:\Test.png”、“C:\Test.ico”、FreeImageAPI.FREE\u IMAGE\u FORMAT.FIF\u ico)
“FreeImageHelper.Convert(新位图(“C:\Test.png”)、“C:\Test.jpg”、FreeImageAPI.FREE\u IMAGE\u FORMAT.FIF\u JPEG、FreeImageAPI.FREE\u IMAGE\u SAVE\u FLAGS.JPEG\u SUBSAMPLING\u 444或FreeImageAPI.FREE\u IMAGE\u SAVE\u FLAGS.JPEG\u QUALITYSUPERB)
'
'PictureBox1.BackgroundImage=FreeImageHelper.GrayScale(新位图(“C:\Test.bmp”))
'PictureBox1.BackgroundImage=FreeImageHelper.GrayScale(“C:\Test.bmp”)
'
'PictureBox1.BackgroundImage=FreeImageHelper.Resize(新位图(“C:\Test.bmp”),32,32)
'PictureBox1.BackgroundImage=FreeImageHelper.Resize(“C:\Test.bmp”,64128)
'
'PictureBox1.BackgroundImage=FreeImageHelper.Rotate(新位图(“C:\Test.bmp”),90)
'PictureBox1.BackgroundImage=FreeImageHelper.Rotate(“C:\Test.bmp”,-90)
'
'PictureBox1.BackgroundImage=FreeImageHelper.缩略图(新位图(“C:\Test.png”),64,真)
'PictureBox1.BackgroundImage=FreeImageHelper.缩略图(“C:\Test.png”,64,True)
导入FreeImageAPI
公共类FreeImageHelper
' 
'检查FreeImage.dll是否在系统上可用。
' 
公共共享函数可作为布尔值使用
返回FreeImage.IsAvailable
端函数
' 
'获取FreeImage.dll的版本。
' 
共享函数Get_Version()作为字符串
返回FreeImage.GetVersion
端函数
' 
'获取图像文件的图像格式。
' 
共享函数Get_ImageFormat(ByVal文件作为字符串)作为字符串
返回FreeImage.GetFileType(文件,0).ToString.Substring(4)
端函数
' 
'在图像格式之间转换位图对象并将其保存到磁盘。
' 
共享子转换(ByVal bmp作为System.Drawing.Bitmap_
ByVal输出为字符串_
ByVal NewFormat作为自由图像格式_
可选的ByVal SaveFlags As FREE_IMAGE_SAVE_FLAGS=FREE_IMAGE_SAVE_FLAGS.默认值)
尝试
保存位图(bmp、输出、新格式、保存标志)
特例
'引发新异常(例如消息)
MsgBox(例如消息)
结束尝试
端接头
' 
'在图像格式之间转换图像文件并将其保存到磁盘。
' 
共享子转换(ByVal文件作为字符串_
ByVal输出为字符串_
ByVal NewFormat作为自由图像格式_
可选的ByVal SaveFlags As FREE_IMAGE_SAVE_FLAGS=FREE_IMAGE_SAVE_FLAGS.默认值)
尝试
保存(新格式,FreeImage.LoadEx(文件),输出,保存标志)
特例
'引发新异常(例如消息)
MsgBox(例如消息)
结束尝试
端接头
' 
'灰度位图对象。
' 
共享函数灰度(ByVal bmp作为System.Drawing.Bitmap)作为System.Drawing.Bitmap
尝试
Dim ImageStream作为新System.IO.MemoryStream
保存(ImageStream,bmp.RawFormat)
将图像变暗为FIBITMAP=FreeImage.LoadFromStream(ImageStream)
Dispose()文件
返回FreeImage.GetBitmap(FreeImage.ConvertToGreyscale(Image))
特例
'引发新异常(例如消息)
MsgBox(例如消息)
一无所获
结束尝试
端函数
' 
'灰度图像文件。
' 
共享函数灰度(ByVal文件为字符串)为System.Drawing.Bitmap
尝试
返回FreeImage.GetBitmap(FreeImage.ConvertToGreyscale(FreeImage.LoadEx(文件)))
特例
'引发新异常(例如消息)
MsgBox(例如消息)
一无所获
结束尝试
端函数
' 
'调整位图对象的大小。
' 
共享函数调整大小(ByVal bmp作为System.Drawing.Bitmap_
ByVal X作为Int32_
ByVal Y作为Int32_
可选的ByVal质量作为FREE\u IMAGE\u FILTER=FREE\u IMAGE\u FILTER.FILTER\u BILINEAR)作为System.Drawing.Bitmap
尝试
Dim ImageStream作为新System.IO.MemoryStream
保存(ImageStream,bmp.RawFormat)
将图像变暗为FIBITMAP=FreeImage.LoadFromStream(ImageStream)
Dispose()文件
返回FreeImage.GetBitmap(FreeImage.Rescale(图像,X,Y,质量))
特例
'引发新异常(例如消息)
MsgBox(例如消息)
一无所获
结束尝试
端函数
' 
'调整图像文件的大小。
' 
共享函数调整大小(ByVal文件为字符串_
ByVal X作为Int32_
ByVal Y作为Int32_
可选的ByVal质量作为FREE\u IMAGE\u FILTER=FREE\u IMAGE\u FILTER.FILTER\u BILINEAR)作为System.Drawing.Bitmap
尝试
返回FreeImage.GetBitmap(FreeImage.Rescale(FreeImage.LoadEx(文件),X,Y,Quality))
特例
“扔掉新的前男友