How to modify an image by inserting another image? - vb.net

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

Related

rotated image in a picturebox shows rotated and original form

I'm a begginer using VB.Net framework 4.7.2 Winforms.
Im trying to rotate an image of a plane in my application , but it shows the rotated one and the original.
left picture is not rotated and the right is rotated at -25°
Private Sub Rotation(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
With e.Graphics
.TranslateTransform(PictureBox1.Width \ 2, PictureBox1.Height \ 2)
.RotateTransform(dgr)
.DrawImage(PictureBox1.Image, (-PictureBox1.Width \ 2), (-PictureBox1.Height \ 2))
End With
End Sub
This is the code I rotate the image with
I want just the rotated image shown.
Thanks in advance.
To elaborate, if you assign an Image object to the Image property of the PictureBox then the control will draw that Image itself every time. If you then draw that same Image object yourself in the Paint event handler then of course you see two images.
Basically, don't assign anything to the Image property of the control but rather to your own Image field and use that in the Paint event handler:
Private myImage As Image
Private Sub Rotation(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
With e.Graphics
.TranslateTransform(PictureBox1.Width \ 2, PictureBox1.Height \ 2)
.RotateTransform(dgr)
.DrawImage(myImage, (-PictureBox1.Width \ 2), (-PictureBox1.Height \ 2))
End With
End Sub
You would set myImage in code wherever you're currently setting PictureBox1.Image or in the Load event handler if you're currently setting it in the designer.

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.

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.
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

Displaying image on clicked location on picturebox in VB.net

I want an image to be inserted when I clicked on specific location on picturebox. the image shows when I put coordinates in number form in the code below.
g.DrawImage(Image.FromFile("myImage.gif"), New Point(10, 10))
But what I want is that the coordinates is retrieved when I click on a specific location and put it on a variable. But the image is not displaying with this code, I just replaced the number 10,10 with a variable.
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
myX = e.X
myY = e.Y
g.DrawImage(Image.FromFile("myImage.gif"),New Point(myX, myY))
End Sub
Why is it not working ? Any idea sirs.
You need coordinates relative to the form. e.X, e.Y And Cursor.Position, they all supply global coordinates. Thus, set the relative coordinates using:
g.DrawImage(Image.FromFile("myImage.gif"), Me.PointToClient(Cursor.Position))

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