Writing a Graphic to file

Hi everyone,
   a graphics question for you:

I have a graphic (not an image) and I'd like to write it to a file.
The image class has a save method that takes a file name the graphic class has simply a save method (no paramters).

I've tried casting my graphic to an image but it doesn't seem to like it, can anyone tell me how I can write my graphic to a given filename

thanks in advance,
Ed.


Answer this question

Writing a Graphic to file

  • KD Hallman-MSFT

    Hi rtpninja ,
        thanks for the reply but I  think you got mixed up  alittle.

    I'm trying to go from a graphic to a bitmap

    The solution you posted would go from a bitmap to an image.

    thanks anyway,
    Ed.

  • Arjan1982

    Hi

      How can I put a Graphic in a Bitmap


    Thanks

  • Imran Aziz

    If I understand your question then here's one way to do it:

    1) Create a Bitmap object of the appropriate size.
    2) Create your Graphics object from the bitmap, using the Graphics.FromImage() method.
    3) Paint onto the Graphics object.
    4) The Bitmap will now contain whatever drawing you did with the Graphics object.
    5) Now you can save the Bitmap and specify a file name.

    Hope this helps.


  • Steven Galione

    Okay, this is confusing, but what you are really trying to do is write an image to a file, yes   So the idea is to get that image into a form where you can use the Save() method that takes a file name.  Doesn't my solution allow for that   Ha ha.  Maybe I am confused - but I think I understand what you're trying to do.
  • Qube

    Of course, if the Graphics object you are talking about is associated with a window (you got it by listening to the Paint event, etc) then so far as I know the only way to do that is to create an in-memory bitmap, associate it with an in-memory (a.k.a. "compatible" DC), and BitBlt from the source DC to the memory DC.  Then you can save your in-memory bitmap to file.  But in order to do this you need to leverage some of the Win32 GDI functions.. there are good examples of this around.  Google: "C# screen capture" and you should find a few.  If there's an easier way I'd like to hear about it.


  • Writing a Graphic to file