[英]Convert Picturebox Image to Transparent VB.Net
带有白色背景的图像时出现问题。 如何去除白色背景或使图像透明?
现在我正在使用此代码
Dim _ms3 As New System.IO.MemoryStream()
pbSignCapture.Image.Save(_ms3, System.Drawing.Imaging.ImageFormat.Png)
Dim _arrImage3() As Byte = _ms3.GetBuffer()
_ms3.Close()
还使用_arrImage3
保存图像。
我想在PictureBox中转换图像以将白色背景变成透明的。
考虑使用Bitmap
类打开图像文件。
Dim myImage as new Bitmap("C:\Image file.bmp")
然后可以使用MakeTransparent()或MakeTransparent(Color)方法:
获取背景像素的颜色。
Dim backColor As Color = myImage.GetPixel(1, 1)
使myColormap的backColor透明。
myImage.MakeTransparent(backColor)
编辑:
从新的细节可以理解,您希望PictureBox
透明,而源图像是透明的。 不幸的是,由于透明系统没有级联,因此使用WinForms
是不可能的。 您可以将pictureBox的BackgroundColor
属性设置为透明,但这将与您认为的有所不同。 PictureBox控件的可用像素将显示父控件的内容。
这意味着,例如,如果您在picurebox下方有一个标签,并为图像设置了透明背景; 标签不会显示,因为它不是图片框的父控件。
解决方法是在目标控件的paint
事件中手动绘制图像。
假设您有一个包含许多控件的表单,并且想要在按钮(名为btn)上绘制广告图像。 您必须以这种方式覆盖表单的paint事件:
Private Sub form_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles form.Paint
Dim g As Graphics = e.Graphics
g.DrawImage(Image.FromFile("C:/yourimage.png", btn.Location.X, btn.Location.Y)
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.