picturebox image clearing - vb.net

i created a drawing apllication in my solution by this code.by this i can draw images in a picturebox and can to save. when i click the clearbutton the image on the picturebox is cleared but the problem is after clearing the image i can't draw any thing in the picturebox without the form's reload
dim mousepath as new system.drawing.drawing2d.graphicspath()
in pageload
picturebox1.image=new bitmap(picturebox1.clientsize.height,picturebox1.clientsize.width)
picturebox1_paint(...)
myusercolor=(sysytem.drawing.color.black)
myalpha=100
using g as graphics=graphics.fromimage(picturebox1.image)
g.clear(color.white)
dim currentpen as pen=new pen(color.fromargb(myalpha,myusercolor),mypenwidth) g.drawpath(currentpen,mousepath)
in mousedown
if e.button=mousebutton.left then
mousepath.startfigure()
end if
in mousemove
if e.button=mousebutton.left then
mousepath.addline(e.x,e.y,e.x,e.y)
end if
picturebox1.invalidate
clearbutton_click
picturebox1.image.dispose()
picturebox1.image=nothing
PROBLEM:
if anyone knows the solution for this problem please help me.it's very important for me.thank you

when you clear, you set picturebox1.image=nothing, but then your Graphics object comes from that image. That's why it won't work. You have to set a new Bitmap when you clear like the first time :
picturebox1.image=new bitmap(picturebox1.clientsize.height,picturebox1.clientsize.width)
mousepath.Reset();

Related

Reset picturebox to default image

I have a picturebox. I set the default image to a picture with some word. So it embed in the textbox. So I load this picturebox using code. The when I click on Reset button, I will dispose the image that been hold. So how to set back the default image after I dispose it?
Have you tried something like the following code?
pictureBox1.Image = pictureBox1.InitialImage

Draw Image to Picture Box using AutoScroll but keep a Header Visible

I have a Picturebox which I draw a view to (Gantt View in this case) and it works OK - i.e., the view is drawn and the AutoScroll property allows the image in the PictureBox to be smoothly scrolled.
My problem is, the header of the image (e.g., the date headers in this case) scroll off the top of the display when I scroll down the image.
What I can't work out is how to fix a header to the top. I thought about simply drawing a header into another Picturebox, but then I am not sure how to sync the header with the left-right scrolling of the main PictureBox
Can someone suggest the best approach to handling this, or do I need to revert to doing a direct draw and handle the scrolling myself?
I am using VB with VS 2015.
Many thanks
Phil
Updated - I am now using an off-screen Bitmap, but can someone look at the code below and let me know if there is a faster/better way to do this? It all works, but still learning and so always looking to do things the best way
Public Sub MoveViewPoint(G As Graphics)
' G passed in from controls Paint
G.Clear(Color.WhiteSmoke)
' _Plan is off-screen bitmap of image
' _HeaderHeight is height of the Header area in _Plan
Dim Header_src_rect As New Rectangle(_HScroll.Value, 0, _Plan.Width, _HeaderHeight)
Dim Header_dst_rect As New Rectangle(0, 0, _Plan.Width, _HeaderHeight)
G.DrawImage(_Plan, Header_dst_rect, Header_src_rect, GraphicsUnit.Pixel)
Dim src_rect As New Rectangle(_HScroll.Value, _HeaderHeight + 1 + _VScroll.Value, _Plan.Width, _Plan.Height)
Dim dst_rect As New Rectangle(0, _HeaderHeight + 1, _Plan.Width, _Plan.Height)
G.DrawImage(_Plan, dst_rect, src_rect, GraphicsUnit.Pixel)
_HScroll.LargeChange = G.ClipBounds.Width * 0.9
_VScroll.LargeChange = G.ClipBounds.Height * 0.9
End Sub
I would do all your drawing to an off-screen bitmap using the GDI graphics system. You can draw your headers and the rest of the chart as 2 distinct stages in the same bitmap. You would have to handle the scrolling yourself by watching the MouseMove event and checking the buttons status.

Place a picture box inside another: VS2008

hope i can get some help here.
For a VB app, I try to place one picturebox over GDI+ drawn image (or another picturebox) in visual studio 2008, and the picturebox includes transparent areas which don't come up transparent.
I have looked through many methods but they don't suit my situation:
a background is drawn using GDI, and for my app I need a picturebox stationary upon it. (the stationary image cant be GDI+ drawn too, because the background updates every while, which causes it to disappear. tried to redraw image also but it makes an ugly flickering effect).
when i try to use picturebox as background, and assign it as parent for the overlaying image for transparency effect, the above image doesn't show at all. here is the VB code:
Dim back As PictureBox
Dim silhouetteAs PictureBox
Dim img As Bitmap = New Bitmap(imgPath)
img.MakeTransparent(Color.White)
back = New PictureBox()
silhouette= New PictureBox()
silhouette.Parent = back
silhouette.Size = New Point(width, height)
silhouette.Location = New Point(xpos, ypos)
silhouette.Image = img
silhouette.BackColor = Color.Transparent
back.Parent = MAIN
back.Size = New Point(width, height)
back.Location = New Point(xpos, ypos)
back.BackColor = Color.Blue
where back is the background and silhouette is the on-top image.
can someone please provide an example of how transparency achieved through parenting pictureboxes? or any other solution to this situation?
Never mind, found out what was wrong - when I assigned another picturebox as a parent, I didn't update the location of the overlaying picturebox relatively. the correction would be to
add:
silhouette.location=new point(0,0)
forgot that the coordinates are relative to the parent and not the form anymore.

VB.NET Winforms: Overlay two transparent images

I am attempting to overlay two transparent images within a winform, but it keeps rendering the form's background image behind the top transparent image, as opposed to the second image...
My basic set up is I have two panels and in each panel is a picturebox. Each image in the picture boxes have some transparent areas. I've set the BackColor of the panels to color.transparent.
When I make one panel overlay the other I'm seeing the form's backcolor come through as opposed to the underlaying image.
Am I missing a property that I can set?
You only need one picture box. The overlay can be done with graphics.
Imports System.Drawing
Dim OverlayImage As New Bitmap("Some Path", True)
Dim BackImage As New Bitmap("Some Path", True)
g As Graphics = Graphics.FromImage(BackImage)
g.DrawImage(OverlayImage, 0, 0)
pictureBox1.Image = BackImage
If you want to have the timer move the overlayed image, then first, make a variable Dim posX As Integer = 0
then use g.DrawImage(OverlayImage, posX, 0) Now when your timer ticks, increment posX by 10
Here's a complete function to overlay two images (adapted from Blue0500's answer):
''' <summary> Return a new image with one superimposed over the other. </summary>
Function OverlayImgs(ByVal BackgroundImg As System.Drawing.Bitmap, ByVal OverlayImg As System.Drawing.Bitmap, Position As System.Drawing.Point) As System.Drawing.Bitmap
Dim g = System.Drawing.Graphics.FromImage(BackgroundImg)
g.DrawImage(OverlayImg, Position)
Return BackgroundImg
End Function
Usage:
lblTest.Image = OverlayImgs(Img1, Img2, New Point(16, 16))
You don't need a PictureBox for this unless you are using it for the canvas. You can draw all images to a Rectangle structure and move them around. Personally I would create a class object that has a Rectangle, Image and other properties and hold them in a collection. Then you simply draw the class objects by there properties including location. If there images contain transparencies they will overlay for you. This also gives you a method to check for collisions via the Rectangle.IntersectsWith function.

How to dock a control inside a panel at runtime on windows forms?

I am designing a simple user interface using winforms. In the designer I have a panel on the form and would like to add a datagridview control into the panel at runtime and set the dock property of the datagridview to 'Fill' so that it fills the panel.
I am struggling to do this and following code is not working out for me:
Dim MyDataGridview as New DataGridView()
MyDataGridView.Dock = DockStyle.Fill
Me.MyPanel.Controls.Add(MyDataGridview)
Can anybody help point me in the right direction?
Edit:
Sorry for being vague - I don't get an error but the datagridview isn't visible. It gets added 'behind' the panel and so I tried using .SendToBack() and .BringToFront() methods thinking that the panel was hiding the datagridview but this doesn't seem to work either. At any rate, it seems like the datagridview is being added to the form but just not docked within the panel
Actually, your code ought to work, but you do realize you'll just get a grey rectangle w/o columnheaders etc?
Anyway, the following should be a bit more reliable:
Dim MyDataGridview as New DataGridView()
Me.MyPanel.Controls.Add(MyDataGridview)
MyDataGridView.Dock = DockStyle.Fill
MyDataGridView.Visible = True
Set the Dock property AFTER adding the DataGridView to the panel and then call "BringToFront()" to change the Z-index.
I think that the Z-index part is what makes the trick ;)
This was happening to me, but I had forgotten to un-suspend the layout logic of the container after adding the panel :). So all I needed was:
Container.ResumeLayout() ' docking works now!
Try this:
Dim MyDataGridview as New DataGridView()
MyDataGridView.Parent = Me.MyPanel
MyDataGridView.Dock = DockStyle.Fill
MyDataGridView.Show