今天研究了下.NET窗体应用程序生成透明底色的PNG图像。
我的.NET版本为.NET 4.5,VS版本为VS2012,操作系统版本为Win7旗舰版ServicePack1。
例程1:生成100%纯透明的PNG图片,这个例程生成的是一张没有任何杂质的透明PNG图片。
''' <summary> ''' 生成100%透明的PNG图片 ''' </summary> ''' <param name="Width">横向长度</param> ''' <param name="Height">纵向长度</param> ''' <param name="OutputPath">图片输出地址</param> ''' <remarks></remarks> Public Sub GenTransparentPNG( ByVal width As Integer,ByVal height As Integer,ByVal outputPath As String) Dim image As Bitmap = New Bitmap(width,height) Dim g As Graphics = Graphics.FromImage(image) g.Clear(Color.Transparent) image.Save(outputPath,System.Drawing.Imaging.ImageFormat.Png) End Sub
''' <summary> ''' 生成100%透明的PNG图片+一个矩形 ''' </summary> ''' <param name="Width">横向长度</param> ''' <param name="Height">纵向长度</param> ''' <param name="OutputPath">图片输出地址</param> ''' <remarks></remarks> Public Sub GenTransparentPNGAndRect( ByVal width As Integer,height) Dim g As Graphics = Graphics.FromImage(image) g.Clear(Color.Transparent) If width > 20 And height > 20 Then g.DrawRectangle(Pens.Black,New Rectangle(10,10,width - 20,height - 20)) End If image.Save(outputPath,System.Drawing.Imaging.ImageFormat.Png) End Sub
''' <summary> ''' 生成白色底色的PNG图片+一个矩形 ''' </summary> ''' <param name="Width">横向长度</param> ''' <param name="Height">纵向长度</param> ''' <param name="OutputPath">图片输出地址</param> ''' <remarks></remarks> Public Sub GenWhitePNGAndRect( ByVal width As Integer,height) Dim g As Graphics = Graphics.FromImage(image) g.Clear(Color.White) If width > 20 And height > 20 Then g.DrawRectangle(Pens.Black,System.Drawing.Imaging.ImageFormat.Png) End Sub
例程4:将指定颜色替换为透明,如果有需要还可以将此例程进行改造,使得在一定范围内的颜色都被替换为透明。
''' <summary> ''' 将指定颜色替换为透明 ''' </summary> ''' <param name="bitmapPath">被替换图像地址</param> ''' <param name="outputPath">新图像输出地址</param> ''' <param name="color">被替换颜色</param> ''' <remarks></remarks> Public Sub ChangeBackgroundToTransparent( ByVal bitmapPath As String,ByVal outputPath As String,ByVal color As Color) Dim image As Bitmap = New Bitmap(bitmapPath) For i As Integer = 0 To image.Width - 1 For j As Integer = 0 To image.Height - 1 Dim colorTmp = image.GetPixel(i,j) If colorTmp.R = color.R And colorTmp.G = color.G And colorTmp.B = color.B Then image.SetPixel(i,j,Drawing.Color.Transparent) End If Next Next image.Save(outputPath,System.Drawing.Imaging.ImageFormat.Png) End Sub
Sub Main() '生成200*200的纯透明PNG图片 GenTransparentPNG(200,200,"C:\Users\Tsybius\Desktop\transparent.png") '生成200*200的透明PNG图片外加一个矩形 GenTransparentPNGAndRect(200,"C:\Users\Tsybius\Desktop\transparentRect.png") '生成200*200的白色背景PNG图像外加一个矩形 GenWhitePNGAndRect(200,"C:\Users\Tsybius\Desktop\whiteRect.png") '将200*200的白色背景PNG图像中白色抠去替换为透明 GenWhitePNGAndRect(200,"C:\Users\Tsybius\Desktop\whiteRect2.png") ChangeBackgroundToTransparent("C:\Users\Tsybius\Desktop\whiteRect2.png","C:\Users\Tsybius\Desktop\transparentRect2.png",Color.White) '生成完毕 Console.WriteLine("生成完毕") Console.ReadLine() End Sub
运行结果如下:
附上另外两个实用的小技巧:
1、获取图像指定点的RGB值,用QQ截图工具,按下Ctrl+Alt+A进入截图模式后,鼠标右下角会自动显示出当前鼠标指针指向位置的颜色信息。
在截图模式下,按下Ctrl键,可以看到十六进制显示的RGB信息
2、判断图像的背景色是否为透明的,可以打开一个PowerPoint演示文稿,设置一个背景,然后将要判断的图像放入即可。
我使用的Office版本为:Microsoft Office Professional Plus 2010,PPT版本为:14.0.4760.1000(32位)。
END
原文链接:https://www.f2er.com/vb/256945.html