Convert Graphic to image

  • Thread starter Thread starter Kalvin
  • Start date Start date
K

Kalvin

I am trying to create an image from a graphics object. I would like to
create the image and use in the application without having to save to
disk.

If this isn't possible, please just tell me so I can quite looking.

Thank you
Kalvin
 
Kalvin said:
I am trying to create an image from a graphics object. I would like to
create the image and use in the application without having to save to
disk.

I am not sure what you want to do. You can determine a 'Graphics' object
for an image using 'Graphics.FromImage':

\\\
Dim b As New Bitmap(...) ' Take a look at the overloaded constructor.
Dim g As Graphics = Graphics.FromImage(b)
....
g.Dispose()
....
///

Image data is stored in an image, whereas the 'Graphics' object serves as a
tool for manipulating an image.
 
I am trying to create an image to put in a cell of the C1flexgrid
control. There isn't an image already there to get and draw on. I
would like to create a graphics object and draw the parts on it that I
want, then convert the graphic to an image to put into the cell. Here
is something I am trying but am getting errors with it:

Here I am just trying to get the procedure down for creating the image
before I start messing with trying to put it into the grid cell.

'***** CODE STARTS HERE *****
Dim NewFont As New Font(fg.Font.FontFamily, fg.Font.Size / 2,
FontStyle.Regular)

Dim pic As Bitmap = New Bitmap(box.Width, box.Height _
, Imaging.PixelFormat.Format24bppRgb)

g = Graphics.FromImage(pic)
g.SmoothingMode = SmoothingMode.AntiAlias

g.DrawString("H", NewFont, Brushes.Blue, 0, 0)

Dim st As System.IO.Stream

pic.Save(st, System.Drawing.Imaging.ImageFormat.Bmp)
Dim img As Image = Image.FromStream(st)

'**** END OF CODE ****

I keep getting told that st is nothing when I try to save to it, but
this a must inherit object.

Am I missing the boat completely here?
Kalvin
 
I think I found a solution, is this a bad idea for some reason?

If I use a MemoryStream:
Dim st As New System.IO.MemoryStream

then I don't get an error and it works.

Kalvin
 
Kalvin said:
I think I found a solution, is this a bad idea for some reason?

If I use a MemoryStream:
Dim st As New System.IO.MemoryStream

then I don't get an error and it works.

Sure, you can save the image to a memory stream instead of saving it to a
file.
 

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

Back
Top