[cairo] Creating Win32 Bitmap with cairo

Isenko Evgeny isenko at po.gsinet.co.jp
Thu Nov 6 01:25:32 PST 2008

>> Does anyone have an experience of creating Win32 Bitmap with cairo?
>> The below example is working just fine except one thing: the resulting
>> Bitmap is not antialiased. However, if I save the surface into png,
>> imgsrf.WriteToPng(fName);
>> it IS antialiased.
>> So, how make the graphics on the Bitmap to be antialiased?
>> {
>> Bitmap img = new Bitmap(200, 200, Imaging.PixelFormat.Format32bppArgb);
>> Graphics mygr = Graphics.FromImage(img);
>> Cairo.Win32Surface winsrf = new Cairo.Win32Surface(mygr.GetHdc());
> Are you sure you don't mean transparency, and not antialiasing?
I mean antialiasing.
> Note that if you create a cairo win32 surface from a HDC, cairo always 
> assumes that that surface is just 24bpp, with no alpha.  If you want a 
> bitmap with alpha, you need to have cairo create it, via 
> cairo_win32_surface_create_with_dib.
Yes, I wanted to use cairo_win32_surface_create_with_dib, but Mono.Cairo 
(for C#), which I use, doesn't seem to implement it.

But anyway I found a solution, which works perfect. I tryed ImageSurface 
instead of Win32Surface and that was successful!

The code is below. I spent half a day to get this working :-)

Bitmap img = new Bitmap(Width, Height, Imaging.PixelFormat.Format32bppArgb);
Imaging.BitmapData bmpData =
                img.LockBits(new Rectangle(0, 0, Width, imgHeight),
                Imaging.ImageLockMode.WriteOnly, img.PixelFormat);

Cairo.ImageSurface imgsrf = new Cairo.ImageSurface(bmpData.Scan0,
 Cairo.Format.Argb32, Width, Height, Width * 4);
cr = new Cairo.Context(imgsrf);

/* Some drawings here */

return img;

The problem is resolved.
Thanks to everyone!


More information about the cairo mailing list