Convert Picturebox Image to Transparent VB.Net - 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.
I want to convert the image in the PictureBox to turn the White Background into transparent.

Consider using the Bitmap class to open your image files.
Dim myImage as new Bitmap("C:\Image file.bmp")
And then you can use the MakeTransparent() or MakeTransparent(Color) methods:
Get the color of a background pixel.
Dim backColor As Color = myImage.GetPixel(1, 1)
Make backColor transparent for myBitmap.
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. Unfortunately this is not possible using WinForms because the transparency system is not cascading. You can set the BackgroundColorproperty of pictureBox to transparent, but this is going to act differently from what you may think. The free pixels of the PictureBox control will show the content of the parent control.
It means that if you have, for example, a label below your picurebox and set transparent background to the image; 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.
Let's assume that you have a form with many controls and you want to draw ad image over a button (named btn). You'll have to override the form's paint event this way:
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

Related

How to modify an image by inserting another image?

I have an image (800x800p) loaded in a PictureBox. It's slightly interactive, clicking somewhere on it will show tooltips or popup windows.
Occasionally, clicking would result in adding of 100x100 px image on the location where it was clicked. That modification needs to be saved onto the main image.
Any ideas as to how to do this?
Draw the new image over your original image.
For example:
Public Class Form1
Private Sub PictureBox1_Click(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
Dim NewIMG As New Bitmap(100, 100)
Graphics.FromImage(NewIMG).Clear(Color.Black) 'just to make my image black since I don't have any image
'You drawing new image here
Graphics.FromImage(PictureBox1.Image).DrawImage(NewIMG, New Point(e.X - 50, e.Y - 50)) 'mouse position - half new image size so that new image center is placed at cursor location
PictureBox1.Refresh() 'to make picbox redraw and display new image
End Sub
End Class

Process memory stacking when drawing bitmap

I have a small application that draws pressure vessels. its not much, just drawing rectangles and arcs.
The user provides input and presses a button to generate the drawing.
But if you keep jamming the button, the process memory increases with each button press. How can I avoid this?
Is the image not disposed by placing a new one in to the picture box?
Private Sub btnRedraw_Click(sender As Object, e As EventArgs) Handles btnRedraw.Click
Dim oBlackPen As New Pen(Color.Black, oBitmapHeight / 1000)
If Not pbVessel.Image Is Nothing Then
pbVessel.Image.Dispose()
End If
Dim oImage As Bitmap = New Bitmap(Convert.ToInt16(oBitmapWidth), Convert.ToInt16(oBitmapHeight))
Dim oGraphics As Graphics = Graphics.FromImage(oImage)
' Le fancy drawing code
pbVessel.Image = oImage
End Sub
Edit: I dispose the picturebox image if the image is not nothing, the problem is still persistent.

vb.net Picture box with a .png with a transparent background loaded in it?

Ive got a .png file called donkey1.png - it has a transparent background and I have loaded it into a picturebox called pcbDonkey1 - I have changed the properties of the picturebox to have the backcolor transparent - This does not work as it still crosses over another image and has a white background.
I've heard about using GDI to draw this image so it will have a transparent image and be able to cross the over image without the white background.
How would you do this?
Thanks
Just set the background picture to be the parent of the foreground picturebox and the transparancy will work without need for any additional coding
With PictureBox1
.Image = My.Resources._00_lichaam
.SizeMode = PictureBoxSizeMode.Zoom
End With
With PictureBox2
.Parent = PictureBox1
.Image = My.Resources._01_Hoofd
.SizeMode = PictureBoxSizeMode.Zoom
.BackColor = Color.Transparent
End With
That should work
Unless you need the PixtureBox control for something more than displaying an image, you can draw the image directly to the form in the Paint event:
Example (you will need to calculate aspect ratio etc., but in principle):
Sub Form_Paint(s as Object, e as PaintEventArgs) Handles Me.OnPaint
Dim r As New Rectangle(myX, myY, myWidth, myHeight)
e.graphics.DrawImage(myImage, r)
End Sub

Darken a .Net Form

I have a 1080p touchscreen application. When a modal pops up, i want to emphasize that by darkening the main form.
Right now i use a second form, the size of the main form, that is black and has 50% opacity. Whenever a modal needs to appear, i open the opaque form, and then open the desired modal.
I feel this is a bit devious for my purpose. Its also not asshole-proof that when the user alt tabs, the forms will glitch out of sequence.
Is there a better way to achieve the darkening effect. Perhaps by darkening the main form from within itself?
Solved it myself by doing the following:
Place a hidden picturebox with dock:fill on the main form,
Take a screenshot of the current screen and darken it
assign the image to the picturebox and make it visible
open the modal in a new win
when the modal is dismissed
hide the picturebox
It really stupid that VB.net doesn't have this function built into it. Here's what you do to get around it:
Make a new form and call it Shade. I'm going to assume your main form is called frmMain. For the sake of clarity, lets assume the form you're launching is called dlgX.
Add the following lines in the Load event of dlgX (that's the sub with dlgX.Load or Me.Load or MyBase.Load):
Shade.Opacity = 0.001
Shade.Show()
Shade.Location = frmMain.Location ' Form location will only update if the form is visible.
Shade.Hide()
Shade.FormBorderStyle = Windows.Forms.FormBorderStyle.None 'This gets rid of the windows Titlebar and window border.
Shade.Size = frmMain.Size
Shade.BackColor = Color.Black
Shade.Opacity = 0.5
Shade.Show() ' Form size will only update the next time you show it.
Shade.TopMost = True ' Puts Shade over main form
Me.TopMost = True ' Puts current form over shade
Under all events that dismiss the form dlgX (OK.click, Cancel.click, etc), add the following lines:
Shade.Close
Or you can even make your own sub that handles all events where the form is closed:
Private Sub DispelShades(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosed
Shade.Close()
End Sub
This is way simpler than the PictureBox scenario and you don't have to mess with layering issues and having to ensure that the PictureBox renders on top of everything (for example, tabs really do not like having things rendered above them and they will not let you render a picture box above them). Rendering a black semi transparent form above your main form gets around all these headaches.
If you have multiple forms to shade, just make a Shad1, Shade2, Shade3 etc.
This is pretty obvious but it's worth stating: if you're shading the main form, you'll also want to make it unclickable by opening dlgX via dlgX.ShowDialog and not dlgX.Show
Here is some code, very similar to the method in Thomas's answer. Note to use the Darkness property in a Try...Finally block, to make sure you never leave the form in the dark state.
Public Class Form1
Private _PB As PictureBox
Public WriteOnly Property Darkness
Set(value)
If value Then
Dim Bmp = New Bitmap(Bounds.Size.Width, Bounds.Size.Height)
Me.DrawToBitmap(Bmp, New Rectangle(Point.Empty, Bounds.Size))
Using g = Graphics.FromImage(Bmp)
Dim Brush As New SolidBrush(Color.FromArgb(125, Color.Black))
g.FillRectangle(Brush, New Rectangle(Point.Empty, Bmp.Size))
End Using
_PB = New PictureBox
Me.Controls.Add(_PB)
_PB.Size = Bounds.Size
_PB.Location = Bounds.Location - PointToScreen(Point.Empty)
_PB.Image = Bmp
_PB.BringToFront()
Else
If _PB IsNot Nothing Then
Me.Controls.Remove(_PB)
_PB.Dispose()
End If
End If
End Set
End Property
Private Sub btnDialog_Click(sender As Object, e As EventArgs) Handles btnDialog.Click
Try
Darkness = True
MsgBox("Modal dialog")
Finally
Darkness = False
End Try
End Sub
End Class

Background Image on Panel flicker when scrolling

I've got a panel in windows forms (Visual Studio 2008) which has a background image (A book shelf).
When scrolling the image flickers and does not redraw so looks awfully - I've tried creating a new object to use double buffering but this has no effect, any suggestions?
Public Class DoubleBufferPanel
Inherits Panel
Public Sub New()
Me.DoubleBuffered = True
SetStyle(ControlStyles.AllPaintingInWmPaint Or _
ControlStyles.DoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)
UpdateStyles()
End Sub
End Class
That seems to happen when the BackgroundImageLayout is set to Zoom. If you set it to Stretch, that might fix the problem. If necessary, you can resize the image to fit the panel at load time and whenever the panel size changes.
I have used a docked picturebox, rather than labels I added the text directly to the image.