Rolf Marvin Bøe Lindgren wrote:
[David C. Fox]
> Well, you
> > (1) create an empty bitmap,
> (2) use SelectObject to identify it with a memory DC,
> (3) convert it to an image,
> > and
> > (4) save the image,
> > but you never copy anything from the DC on which you painted the graph
> to the memory DC, so of course the exported file is an empty gif
> :-). You need to copy from the screen or client DC into the memory DC
> before step (3):
hi, and thanks for your reply.
I'm mailing directly to you as I don't suppose this is of general
interest to the list. I'm really struggling to understand this and the
code I've written is largely taken from tutorials.
(Actually, it looks like your reply went back to the list - but that's probably just as well, since I'm not sure I know the answer)
I create something with
dc = wxPaintDC(self) ... dc.EndDrawing()
but I never reference the object dc after the drawing is finished. so
is there a place in memory that is just reserved for drawings, so that I
can have only one drawing at one given time? as far as I understand
I'm not really an expert on device contexts, but as I understand it, the wxPaintDC is a special purpose DC which is only accessible from within the Paint event handler (usually called OnPaint). So it is perfectly normal not to refer to the paint DC again once you've called EndDrawing.
If you want to create a drawing off-screen, you use a wxMemoryDC, select a bitmap into it, and then draw into the memory DC. Then, to display that drawing, you can use the Blit method to copy it into the PaintDC in OnPaint.
> can have only one drawing at one given time? as far as I understand
what I do is copy the bitmap from screen memory.
I think I've followed your suggestion, like this:
# get the size of the drawing
(thisScreenX, thisScreenY) = self.GetClientSizeTuple()
# create an empty bitmap
self.bitmap = wxEmptyBitmap(thisScreenX,thisScreenY)
# identify the empty bitmap with a memory DC
self.memoryCanvas = wxMemoryDC()
self.memoryCanvas.SelectObject(self.bitmap)
# get the DC representing the client area of the window (self)
client = wxClientDC(self)
# copy a section with the right width and height from 0, 0 in
# the client DC to 0, 0 in the memory DC
self.memoryCanvas.Blit(0, 0, thisScreenX, thisScreenY, client, 0,0, wxCOPY, true)
# convert the bitmap to an image
self.image = wxImageFromBitmap(self.bitmap)
# save the image
self.image.SaveFile("plopp.gif",wxBITMAP_TYPE_GIF)
the result is still an empty file.
Oh, wait, I think I remember that you can't save GIFs (they can't include code to save GIFs in wxWindows because of the company that has a patent on the GIF compression algorithm, I think). Try JPEG or PNG.
David