Gtk# in Windows

hacking No Comments

I’m hacking on my Gtk# app in Windows right now, because Visual Studio is amazing. I
couldn’t seem to get pixbufs to load from relative paths, because I couldn’t figure out what the present working directory is on anything in Windows, so I started fooling with the resource manager.

This is a very cool thing. You can put strings or images or whatever you want in there, then access it from your app as Properties.Resources.Foo. Very nice.

However, if you’re using images then they’re obviously stored as a System.Drawing.Bitmap, and Gtk# wants to deal with things as a Gdk.Pixbuf. So here’s a quick snippet of code to deal with that:

using System;
using System.Drawing;
using System.Drawing.Imaging;

public Gdk.Pixbuf CreateFromResource( Bitmap bitmap ) {
BitmapData data = bitmap.LockBits( new Rectangle( 0, 0,
bitmap.Width, bitmap.Height,
PixelFormat.Format24bppRgb );
IntPtr scan = data.Scan0;
int size = bitmap.Width * bitmap.Height * 3;
byte[] bdata = new byte[ size ];

Gdk.Pixbuf pixbuf = null;

unsafe
{
byte* p = (byte*)scan;
for( int i = 0; i