Cairo, GraphicsContext and Bitmaps

Hello,

I'm playing for first time with Cairo and i found the following issue:

I try render a bitmap image on a Cairo context (WindowDC) with:

     def OnPaint(self, evt):
         dc = wx.BufferedPaintDC(self)
         dc.SetBackground(wx.Brush('white'))
         dc.Clear()
         ctx = wx.lib.wxcairo.ContextFromDC(dc)

         bmp = wx.Bitmap('c:\\temp\\image.png')
  print "bmp:", bmp.GetSize(), bmp.GetDepth(), bmp.HasAlpha()
         gc.DrawBitmap(bmp, 10,10)

If the image file has Alpha channel, the image is displayed ok, but if i try with a png without alpha or with a jpg file i got: RuntimeError: Failed to gain raw access to bitmap data.

  File "prueba2.py", line 154, in RenderGC
     gc.DrawBitmap(bmp, 10,10)
   File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx\lib\graphics.py", line 1308, in DrawBitmap
     bmp = GraphicsBitmap.CreateFromBitmap(bmp)
   File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx\lib\graphics.py", line 476, in CreateFromBitmap
     b._surface = wx.lib.wxcairo.ImageSurfaceFromBitmap(bitmap)
   File "c:\python25\lib\site-packages\wx-2.8-msw-unicode\wx\lib\wxcairo.py", line 216, in ImageSurfaceFromBitmap
     bitmap.CopyToBuffer(surface.get_data(), fmt, stride)
   File "c:\python25\lib\site-packages\wx-2.8-msw-unicode\wx\_gdi.py", line 765, in CopyToBuffer
     return _gdi_.Bitmap_CopyToBuffer(*args, **kwargs)
RuntimeError: Failed to gain raw access to bitmap data.

Platform: Windows XP SP3
wxPython: '2.8.10.1 (msw-unicode)'
Python 2.5.4

···

--
*****************************************
Oswaldo Hern�ndez
oswaldo (@) soft-com (.) es
*****************************************
PD:
Antes de imprimir este mensaje, aseg�rese de que es necesario.
El medio ambiente est� en nuestra mano.

Oswaldo Hern�ndez wrote:

Hello,

I'm playing for first time with Cairo and i found the following issue:

I try render a bitmap image on a Cairo context (WindowDC) with:

     def OnPaint(self, evt):
         dc = wx.BufferedPaintDC(self)
         dc.SetBackground(wx.Brush('white'))
         dc.Clear()
         ctx = wx.lib.wxcairo.ContextFromDC(dc)

         bmp = wx.Bitmap('c:\\temp\\image.png')
  print "bmp:", bmp.GetSize(), bmp.GetDepth(), bmp.HasAlpha()
         gc.DrawBitmap(bmp, 10,10)

If the image file has Alpha channel, the image is displayed ok, but if i try with a png without alpha or with a jpg file i got: RuntimeError: Failed to gain raw access to bitmap data.

  File "prueba2.py", line 154, in RenderGC
     gc.DrawBitmap(bmp, 10,10)
   File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx\lib\graphics.py", line 1308, in DrawBitmap
     bmp = GraphicsBitmap.CreateFromBitmap(bmp)
   File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx\lib\graphics.py", line 476, in CreateFromBitmap
     b._surface = wx.lib.wxcairo.ImageSurfaceFromBitmap(bitmap)
   File "c:\python25\lib\site-packages\wx-2.8-msw-unicode\wx\lib\wxcairo.py", line 216, in ImageSurfaceFromBitmap
     bitmap.CopyToBuffer(surface.get_data(), fmt, stride)
   File "c:\python25\lib\site-packages\wx-2.8-msw-unicode\wx\_gdi.py", line 765, in CopyToBuffer
     return _gdi_.Bitmap_CopyToBuffer(*args, **kwargs)
RuntimeError: Failed to gain raw access to bitmap data.

Platform: Windows XP SP3
wxPython: '2.8.10.1 (msw-unicode)'
Python 2.5.4

How about this?

img = wx.ImageFromBitmap(bmp)

if not img.HasAlpha():
    img.InitAlpha()

bmp = wx.BitmapFromImage(img)

obviously you don't have to keep doing this over and over in the paint handler, just once someplace will do. By the way, I'm not sure if this *will* work, just a guess :slight_smile:

regards,

···

--
Steven Sproat, BSc
http://www.basicrpg.com/

Steven Sproat escribi�:

Oswaldo Hern�ndez wrote:

Hello,

.....

How about this?

img = wx.ImageFromBitmap(bmp)

if not img.HasAlpha():
    img.InitAlpha()

bmp = wx.BitmapFromImage(img)

obviously you don't have to keep doing this over and over in the paint handler, just once someplace will do. By the way, I'm not sure if this *will* work, just a guess :slight_smile:

Works!! :slight_smile:

Thanks Steven

···

--
*****************************************
Oswaldo Hern�ndez
oswaldo (@) soft-com (.) es
*****************************************
PD:
Antes de imprimir este mensaje, aseg�rese de que es necesario.
El medio ambiente est� en nuestra mano.