PictureBox & GDI

L

Lou

I have a picture box with an image loaded into it from an Image list (Its a
solid Black Image).
Next I draw some lines into the picture box.

The resulting image with the lines is NOT the return value of
PictureBox1.Image????
In the exapmle below control "D" shows only the picture(Black Image) without
the lines I added using the brush?
The Picturebox shows the lines nicely but the lines don't seem to be part of
the controls "Inage" property.
How can I get the lines into my control "D"

-Lou

'Draw lines in the picturebox

'==========================================

Dim m_Brush As Brush

Dim myGraphics As Graphics

Dim m_Color1 As Color = Color.Blue

' Create a solid brush based on selected color

Dim mySolidBrush As New SolidBrush(m_Color1)

' Set m_Brush equal to the newly created brush

m_Brush = mySolidBrush

Dim myPen As New Pen(m_Brush, 2)

' Use the brush to draw the appropriate Drawing in the PictureBox1

myGraphics = PictureBox1.CreateGraphics()

' draw the safe title

myGraphics.DrawLine(myPen, 10, 0, 10, PictureBox1.Height)

myGraphics.DrawLine(myPen, 0, 10, PictureBox1.Width, 10)

myGraphics.DrawLine(myPen, PictureBox1.Width - 10, 0, PictureBox1.Width -
10, PictureBox1.Height)

myGraphics.DrawLine(myPen, 0, PictureBox1.Height - 10, PictureBox1.Width,
PictureBox1.Height - 10)

'===========================================

D.Picture = PictureBox1.Image
 
C

Chris

Lou said:
I have a picture box with an image loaded into it from an Image list (Its a
solid Black Image).
Next I draw some lines into the picture box.

The resulting image with the lines is NOT the return value of
PictureBox1.Image????
In the exapmle below control "D" shows only the picture(Black Image) without
the lines I added using the brush?
The Picturebox shows the lines nicely but the lines don't seem to be part of
the controls "Inage" property.
How can I get the lines into my control "D"

-Lou

'Draw lines in the picturebox

'==========================================

Dim m_Brush As Brush

Dim myGraphics As Graphics

Dim m_Color1 As Color = Color.Blue

' Create a solid brush based on selected color

Dim mySolidBrush As New SolidBrush(m_Color1)

' Set m_Brush equal to the newly created brush

m_Brush = mySolidBrush

Dim myPen As New Pen(m_Brush, 2)

' Use the brush to draw the appropriate Drawing in the PictureBox1

myGraphics = PictureBox1.CreateGraphics()

' draw the safe title

myGraphics.DrawLine(myPen, 10, 0, 10, PictureBox1.Height)

myGraphics.DrawLine(myPen, 0, 10, PictureBox1.Width, 10)

myGraphics.DrawLine(myPen, PictureBox1.Width - 10, 0, PictureBox1.Width -
10, PictureBox1.Height)

myGraphics.DrawLine(myPen, 0, PictureBox1.Height - 10, PictureBox1.Width,
PictureBox1.Height - 10)

'===========================================

D.Picture = PictureBox1.Image


You will need to change the underlying bitmap image to do what you want.
You are actually just drawing on top of the image control by using
drawline.

Take a look at this example. It should get you on your way:
http://www.vbdotnetheaven.com/Code/Jun2003/2058.asp

Good Luck
Chris
 
H

Herfried K. Wagner [MVP]

Lou said:
I have a picture box with an image loaded into it from an Image list (Its a
solid Black Image).
Next I draw some lines into the picture box.

The resulting image with the lines is NOT the return value of
PictureBox1.Image????
In the exapmle below control "D" shows only the picture(Black Image)
without the lines I added using the brush?
The Picturebox shows the lines nicely but the lines don't seem to be part
of the controls "Inage" property.
How can I get the lines into my control "D"

<URL:http://www.dotnetrix.co.uk/misc.html>
-> "Save the Image currently displayed in a PictureBox."
 
L

Lou

I just tried that code and it doesn't save the lines I added to the Picture
box, it only saves the image??
I stepped throgh the code
-Lou
 
H

Herfried K. Wagner [MVP]

Lou said:
I just tried that code and it doesn't save the lines I added to the Picture
box, it only saves the image??
I stepped throgh the code

The sample will only work if you are drawing the lines in the control's
overridden 'OnPaint' method or 'Paint' event handler.
 
L

Lou

Yup its there, but it still saves w/o my lines that I do see in the PB

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

'==========================================

Dim m_Brush As Brush

Dim myGraphics As Graphics

Dim m_Color1 As Color = Color.Blue

' Create a solid brush based on selected color

Dim mySolidBrush As New SolidBrush(m_Color1)

' Set m_Brush equal to the newly created brush

m_Brush = mySolidBrush

Dim myPen As New Pen(m_Brush, 2)

' Use the brush to draw the appropriate Drawing in the PictureBox1

myGraphics = PictureBox1.CreateGraphics()

' draw the safe title

myGraphics.DrawLine(myPen, 10, 0, 10, PictureBox1.Height)

myGraphics.DrawLine(myPen, 0, 10, PictureBox1.Width, 10)

myGraphics.DrawLine(myPen, PictureBox1.Width - 10, 0, PictureBox1.Width -
10, PictureBox1.Height)

myGraphics.DrawLine(myPen, 0, PictureBox1.Height - 10, PictureBox1.Width,
PictureBox1.Height - 10)

'===========================================

End Sub
 
L

Lou

It all works well except when I try to save the bitmap the second time, the
savefile function crashes on the
bmp.save
seems the dispose function doesn't release the file and when you try to
save again it crashes.
Something keeps the file open?
any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top