Convert e.Graphics to an image or bitmap

  • Thread starter Thread starter Charles A. Lackman
  • Start date Start date
C

Charles A. Lackman

Hello,

I have created a complete PrintDocument and need to create an image from it.
How is this done?

e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
e.Graphics.DrawString(Line1.Text, FontLine1, TheBrush, Thelocation1, 390 +
yPos, AStringFormat)
e.Graphics.DrawString(Line2.Text, FontLine2, TheBrush, Thelocation2,
TheHeight1 + (390 + yPos))
e.Graphics.DrawString(Line3.Text, FontLine3, TheBrush, Thelocation3,
TheHeight2 + (390 + yPos))
e.Graphics.DrawString(Line4.Text, FontLine4, TheBrush, Thelocation4,
TheHeight3 + (390 + yPos), AStringFormat)

Dim AnImage as Image

AnImage = CType(e.graphics, Image) does not work.

Any Suggestions?

Thanks,

Chuck
 
* "Charles A. Lackman said:
I have created a complete PrintDocument and need to create an image from it.
How is this done?

e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
e.Graphics.DrawString(Line1.Text, FontLine1, TheBrush, Thelocation1, 390 +
yPos, AStringFormat)
e.Graphics.DrawString(Line2.Text, FontLine2, TheBrush, Thelocation2,
TheHeight1 + (390 + yPos))
e.Graphics.DrawString(Line3.Text, FontLine3, TheBrush, Thelocation3,
TheHeight2 + (390 + yPos))
e.Graphics.DrawString(Line4.Text, FontLine4, TheBrush, Thelocation4,
TheHeight3 + (390 + yPos), AStringFormat)

Dim AnImage as Image

AnImage = CType(e.graphics, Image) does not work.

You will have to create a 'Graphics' object from a bitmap of appropriate
size:

\\\
Dim b As New Bitmap(...)
Dim g As Graphics = Graphics.FromImage(b)
g.SmoothingMode = ...
....
g.Dispose()
b.Save(...)
b.Dispose()
///
 
Create a sub thats takes graphics as a parameter and put your drawing code
in it. For printing, pass the printer graphics object. For an image, create
a bitmap and pass Graphics.FromImage(bitmap). You can then save the bitmap
etc.

James
 
Hello,

Ok, I already have a sub that takes the graphics object

Public Sub MakeGraphic(ByVal AGraphic As Graphics)
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
e.Graphics.DrawString(Line1.Text, FontLine1, TheBrush, Thelocation1, 390 +
yPos, AStringFormat)
e.Graphics.DrawString(Line2.Text, FontLine2, TheBrush, Thelocation2,
TheHeight1 + (390 + yPos))
e.Graphics.DrawString(Line3.Text, FontLine3, TheBrush, Thelocation3,
TheHeight2 + (390 + yPos))
e.Graphics.DrawString(Line4.Text, FontLine4, TheBrush, Thelocation4,
TheHeight3 + (390 + yPos), AStringFormat)

SaveBitMap = New Bitmap(ThePic.Width, ThePic.Height, e.Graphics)
SaveBitMap.Save("C:\Test1.jpg")
End sub

To do the Graphics.FromImage(Bitmap) where is the Bitmap coming from?
Doing the above makes a file with 88k or more but it is empty??

Thanks,
Chuck
 
Thanks, It worked

Chuck

Herfried K. Wagner said:
You will have to create a 'Graphics' object from a bitmap of appropriate
size:

\\\
Dim b As New Bitmap(...)
Dim g As Graphics = Graphics.FromImage(b)
g.SmoothingMode = ...
...
g.Dispose()
b.Save(...)
b.Dispose()
///
 
Dim objBitmap as bitmap = new bitmap(ThePic.Width, ThePic.Height)

MakeGraphic(Graphics.FromImage(objBitmap))

objBitmap.save("c:\test1.jp,Imaging.ImageFormat.Jpeg)

J
 
Back
Top