简体   繁体   English

将Picturebox图像转换为透明VB.Net

[英]Convert Picturebox Image to Transparent VB.Net

I'm having a problem when it comes to images with white background. 带有白色背景的图像时出现问题。 How can I remove the white background or make the image transparent? 如何去除白色背景或使图像透明?

For now I'm using this code 现在我正在使用此代码

Dim _ms3 As New System.IO.MemoryStream()
pbSignCapture.Image.Save(_ms3, System.Drawing.Imaging.ImageFormat.Png)
Dim _arrImage3() As Byte = _ms3.GetBuffer()
_ms3.Close()

Also saving the image using the _arrImage3 . 还使用_arrImage3保存图像。

I want to convert the image in the PictureBox to turn the White Background into transparent. 我想在PictureBox中转换图像以将白色背景变成透明的。

Consider using the Bitmap class to open your image files. 考虑使用Bitmap类打开图像文件。

Dim myImage as new Bitmap("C:\Image file.bmp")

And then you can use the MakeTransparent() or MakeTransparent(Color) methods: 然后可以使用MakeTransparent()MakeTransparent(Color)方法:

Get the color of a background pixel. 获取背景像素的颜色。

Dim backColor As Color = myImage.GetPixel(1, 1)

Make backColor transparent for myBitmap. 使myColormap的backColor透明。

myImage.MakeTransparent(backColor)

EDIT: 编辑:

As I understand from the new details you want to have a PictureBox to be transparent where the source image is transparent. 从新的细节可以理解,您希望PictureBox透明,而源图像是透明的。 Unfortunately this is not possible using WinForms because the transparency system is not cascading. 不幸的是,由于透明系统没有级联,因此使用WinForms是不可能的。 You can set the BackgroundColor property of pictureBox to transparent, but this is going to act differently from what you may think. 您可以将pictureBox的BackgroundColor属性设置为透明,但这将与您认为的有所不同。 The free pixels of the PictureBox control will show the content of the parent control . PictureBox控件的可用像素将显示父控件的内容。

It means that if you have, for example, a label below your picurebox and set transparent background to the image; 这意味着,例如,如果您在picurebox下方有一个标签,并为图像设置了透明背景; the label won't be shown because it is not theparent control of the picturebox. 标签不会显示,因为它不是图片框的父控件。

A workaround is to manually draw the image in the paint event of the destination control. 解决方法是在目标控件的paint事件中手动绘制图像。

Let's assume that you have a form with many controls and you want to draw ad image over a button (named btn). 假设您有一个包含许多控件的表单,并且想要在按钮(名为btn)上绘制广告图像。 You'll have to override the form's paint event this way: 您必须以这种方式覆盖表单的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM