Continuing work on a game for The Bitters, I'm going to post a couple of pretty elementary but handy GDI+ snippets I've used in C#.

Load and Manipulate an Image from a Resource in C#

First, add the image to your project, and in the properties for the image, change "Build Action" to "Embedded Resource". Then:
using System.Reflection;
using System.IO;
using System.Drawing;


Class MockLoad : Form
{
MockLoad()
{
Assembly a = Assembly.GetExecutingAssembly();
//Set Icon
Stream imgStream = a.GetManifestResourceStream("ProjectName.logo.ico");
Icon = new Icon(imgStream);
//Get image for background
Stream backgroundStream = a.GetManifestResourceStream("ProjectName.Image.jpg");
Image bgImage = Bitmap.FromStream(backgroundStream);
BackgroundImage = bgImage;
//Get thumbnail of image
//Have to set a callback function for GetThumbnailImageAbort
System.Drawing.Image.GetThumbnailImageAbort dummyCallback =
new System.Drawing.Image.GetThumbnailImageAbort(ImageCallback);
Image tNail = bgImage.GetThumbnailImage(Width,Height,dummyCallback,IntPtr.Zero);
}
private bool ImageCallback()
{
//Gotta do this, although to my knowledge it's useless.
return false;
}
}
So there you have it. Load images embedded into the assembly, set form Icon, set form background, and get thumbnails. Hope this helps someone.
Tags: