views:

124

answers:

1

Hi

I have created different shapes like circle/rect etc in my program using BufferedPaintDC on event. Now i want to save the file as I click the saveas button in the menu option. For that I am using memoryDC and save the contents as bmp file.

def Saveas(self,event):

    dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", \
    wx.SAVE | wx.OVERWRITE_PROMPT)
    if dlg.ShowModal() == wx.ID_OK:   # user enters filename as something.bmp
     self.show_bmp = wx.StaticBitmap(self)

     w, h = self.GetClientSize()

     draw_bmp = wx.EmptyBitmap(w, h)

     c = wx.MemoryDC(draw_bmp)

     c.SetBrush(wx.Brush('white'))

     c.Clear()

     c.SetPen(wx.Pen("purple", 15))

     c.DrawRectangle(30,30,60,60)   ### ??????####

     myimage = self.show_bmp.GetBitmap()
     self.filename=dlg.GetFilename()
     name = os.path.join('C:\mad', self.filename)
     myimage.SaveFile(name, wx.BITMAP_TYPE_JPEG)
     self.filename=name
    dlg.Destroy()

Now my problem is how do I get the things drawn by the buffered dc on the " c ", so that they can be then converted to image?? I hope my question is clear.As u can see I am drawing the rectangle on the "c" and that is being converted to an image. But I want to get the shapes created on ONPaint . How do I extract that?

Thanks

A: 

What you're asking is very close to saying you want to take a screenshot. Although technically grabbing a copy of what the window currently looks like is not the same as cloning what the OnPaint does, it's possible it would do the job for you.

If it doesn't work, take note of the technique, including the use of DC.Blit(), as those would be the tools you'd use.

See this wxpython-users mailing list post by Andrea Gavana for image-grabbing code.

Edit: if the problem is that because you're doing all your drawing inside the EVT_PAINT handler, then you probably need a different technique. Draw everything from a different routine to a pre-allocated buffer bitmap. Inside the OnPaint, which is just how the image actually gets to the screen, you don't draw, you just copy the already-drawn bitmap. The buffer bitmap persists between OnPaint calls (and in fact is basically independent of OnPaint), so you can add a Save() routine that works rather simply as well. See the wxPyWiki DoubleBuffererDrawing page for various snippets that will show you how to do that. (Also note, this will be a bit of a learning curve, so ask for more help if it's not enough.)

Peter Hansen
hmmm yeah what I want was "cloning" as u say... so that i can modify the saved file later on ..But that will not be possible if I take the screenshot..
Madhura
Why not? You can just load it back in again... or don't save it... keep it in memory. It's just data. I don't understand the distinction you're making between the two.
Peter Hansen
yes.. I will implement it and clear my confusion. Thanks :)
Madhura
Okay. By the way, you don't need to accept an answer until you're confident it actually works for you. It is nice to upvote any that you find "helpful" though, and remember you can always come back and downvote it again later if you decide it was stupid advice. :)
Peter Hansen
haha :D yeah..Btw I did implement it and its working for screenDC perfectly.. But there is still one problem I am using the buffered dc I created instead of it and it is showing blank image irrespective to what is draw on it.Any suggestions??
Madhura