Creating Graphics Files from a control's Device Context

Thanks. The following seems to work:

    def OnSaveAs(self, event):
        # Create a bitmap object
        Bitmap = wxEmptyBitmap(self.width, self.height, depth=-1)
        # Create a Device Context for the Bitmap
        dc = wxMemoryDC()
        dc.SelectObject(Bitmap)
        # Set the Device Context background to White and Clear it
        dc.SetBackground(wxBrush(wxWHITE))
        dc.Clear()
        # Use the Graphics object's DrawLines method to populate the
bitmap's Device Context
        self.graphic.DrawLines(dc)

        # to save as a BMP
        #Bitmap.SaveFile('test.bmp', wxBITMAP_TYPE_BMP)

        # Add the JPEG Handler
        wxJPEGHandler()

        # Convert from Bitmap to Image
        image = wxImageFromBitmap(Bitmap)
        # now save the image to a file
        image.SaveFile('test.jpg', wxBITMAP_TYPE_JPEG)

Of course, I'm always open to further suggestions, enhancements, etc.

David Woods
Wisconsin Center for Education Research
University of Wisconsin, Madison

···

-----Original Message-----
From: Robin Dunn [mailto:robin@alldunn.com]
Sent: Monday, December 09, 2002 2:01 PM
To: wxPython-users@lists.wxwindows.org
Subject: Re: [wxPython-users] Creating Graphics Files from a control's
Device Context

David Woods wrote:

I would also like to enable my users to be able to save the image as a
graphic file, preferably in JPEG format. I guess I don't understand
wxBitmaps vs. wxImages vs. wxDCs well enough to figure it all out. I have
not found the Demo or examples to be helpful in this area -- they all seem
to start with an image file, and I can't seem to figure out how to convert
the Device Context of my wxScrolledWindow into a wxImage that can be

saved.

I'm trying to create a wxBitmap Object attached to a wxBufferedDC, upon
which I can draw my graph. I think I then need to use
wxBitmap.ConvertToImage() to convert it to a wxImage, which has the
wxImage.SaveFile() method, but get an error message saying that

wxBitmapPtr

has not attribute 'ConvertToImage'.

There are a couple approaches (3a or 3b) to getting the wxBitmap:

1. Create a wxBitmap of the right size with wxEmptyBitmap and create a
wxMemoryDC.

2. Select the bitmap into the dc.

3a. Either Blit from the window's wxCLientDC to the memory dc.
3b. or call your drawing function to draw on the memory dc.

4. Now the bitmap holds the drawn image.

SO save to a file you can convert it to a wxImage with wxImageFromBitmap
and then write to a file with SaveFile.

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org