Skip to content

Instantly share code, notes, and snippets.

@kekekeks
Created June 18, 2014 18:34
Show Gist options
  • Save kekekeks/c6856162282227ed51ed to your computer and use it in GitHub Desktop.
Save kekekeks/c6856162282227ed51ed to your computer and use it in GitHub Desktop.
using System;
namespace Kebrum.Widgets
{
public class CairoBridge:IDisposable
{
private Cairo.ImageSurface m_Surface;
private System.Drawing.Graphics m_Graphics;
private System.Drawing.Bitmap m_Bitmap;
private IntPtr m_Data;
public Cairo.ImageSurface Surface
{
get
{
return m_Surface;
}
}
public System.Drawing.Graphics Graphics
{
get
{
return m_Graphics;
}
}
public System.Drawing.Bitmap Bitmap
{
get
{
return m_Bitmap;
}
}
public CairoBridge (int Width, int Height)
{
int Stride = Width * 4;
m_Data = System.Runtime.InteropServices.Marshal.AllocHGlobal (Width * Height * 4);
m_Bitmap = new System.Drawing.Bitmap (Width, Height, Stride, System.Drawing.Imaging.PixelFormat.Format32bppPArgb, m_Data);
m_Graphics = System.Drawing.Graphics.FromImage (m_Bitmap);
m_Surface = new Cairo.ImageSurface (m_Data, Cairo.Format.Argb32, Width, Height, Stride);
}
public void Dispose ()
{
if (m_Data == IntPtr.Zero)
return;
m_Graphics.Dispose ();
m_Graphics = null;
m_Bitmap.Dispose ();
m_Bitmap = null;
((IDisposable)m_Surface).Dispose ();
m_Surface = null;
System.Runtime.InteropServices.Marshal.FreeHGlobal (m_Data);
m_Data = IntPtr.Zero;
}
~CairoBridge ()
{
Dispose ();
}
private static CairoBridge m_Shared=null;
public void Clear (int Width, int Height)
{
int Stride = m_Bitmap.Width;
unsafe
{
uint* curscan = (uint*)m_Data.ToPointer ();
for (uint y = 0; y < Height; y++)
{
uint* cur = curscan + Stride*y;
for (uint x = 0; x < Width; x++)
{
*cur = 0;
cur++;
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment