[英]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.