I discovered this solution by perusing the source of a game called "1945", and though I don't think my code much resembles that code, I still believe credit is due. Here is a link to where I found the game: http://www.codeproject.com/KB/mobile/CfPocket1945.aspx
/*
* SUMMARY
*
* Implementing Transparency in GDI
* Written by Adam Richards
* www.failcode.com
*
* This example illustrates how to create an image with transparency
* in a .NET GDI (Graphics Device Interface) object using C#.
*
* This was written in Visual Studio 2008 Professional, and uses the
* .NET Framework version 3.5.
*
*
* Copyright (c) 2008, Adam Richards
*
* TERMS OF USE:
* - Use at will
* - Use at your own risk
* - Please give me credit when used in a similar context
*
*/
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
// ^^ This is needed for using the ImageAttributes structure.
namespace TransparencyInGDI
{
public partial class Form1 : Form
{
// Define a couple constants
const int ellipseDiameter = 200;
const int ellipseStrokeWidth = 25;
// Declare a few objects we'll need; they'll be explained as we use them.
Graphics graphicsObject = null;
Bitmap image = null;
ImageAttributes ImageAttributes_Transparency;
Rectangle destRect;
Color transparentColor = Color.FromArgb(255, 0, 255); // Pure Magenta
// A flag used to determine the render state
bool renderTransparently = false;
public Form1()
{
InitializeComponent();
// Initialize the main graphics object
graphicsObject = this.CreateGraphics();
// Define the destination rectangle
destRect = new Rectangle
(
(this.ClientRectangle.Width - ellipseDiameter) / 2, // Horizontal center
15, // 15 from the top
ellipseDiameter, // Width
ellipseDiameter // Height
);
// Create and populate the ImageAttribute struct with the transparent color
ImageAttributes_Transparency = new ImageAttributes();
ImageAttributes_Transparency.SetColorKey(transparentColor, transparentColor);
// Call the function that will create our demo image
CreateImage();
}
///
/// This function creates the image we'll be using to demonstrate our transparency.
/// Note that this image is only created once in the lifetime of this application.
///
public void CreateImage()
{
// Instantiate a new Bitmap object
image = new Bitmap
(
ellipseDiameter + ellipseStrokeWidth, // Width
ellipseDiameter + ellipseStrokeWidth // Height
// NOTE: When drawing a primitive, the stroke's width is centered
// on the shape's specified perimeter/circumference.
// For this reason we'll make our bitmap oversized by
// 2 * (strokeWidth / 2), which is (1 * strokeWidth).
);
// Create a graphics device from our new image and draw to it.
using (Graphics g = Graphics.FromImage(image))
{
g.Clear(transparentColor); // Clear the image with the transparent color.
g.DrawEllipse
(
new Pen(Color.Blue, (float)ellipseStrokeWidth),
ellipseStrokeWidth / 2,
ellipseStrokeWidth / 2,
ellipseDiameter,
ellipseDiameter
// NOTE: Per the NOTE above, we offset the render position of our
// ellipse by 1/2 of the stroke width, so that the very
// outside edges of our ellipse's stroke are drawn within
// the image's boundaries.
);
}
}
///
/// When the "Toggle Transparency" button in our form is clicked, toggle the
/// flag that specifies whether or not to draw the transparent regions, and
/// re-render the scene.
///
private void button1_Click(object sender, EventArgs e)
{
// Flip the transparency-rendering flag
renderTransparently = !renderTransparently;
// Re-render the image
RenderImage();
}
///
/// This function renders the image to the form. It is called when the
/// "Toggle Transparency" button (button1) is clicked.
///
private void RenderImage()
{
// Clear Form1's Graphics object.
graphicsObject.Clear(Color.White);
// Draw a green x before we blit out image so the transparency can be "seen" better...
Pen bgPen = new Pen(Color.Green, 10.0f);
graphicsObject.DrawLine(bgPen, destRect.X, destRect.Y, destRect.Right, destRect.Bottom);
graphicsObject.DrawLine(bgPen, destRect.X, destRect.Bottom, destRect.Right, destRect.Y);
if (!renderTransparently)
{
// This renders the image as created, without transparency,
// using a simple Graphics.DrawImage overload.
graphicsObject.DrawImage
(
image, // The image with the ellipse we created
destRect // The destination rectangle (where in the
// parent form to blit the image).
);
}
else
{
// This renders the image with an overload of the Graphics.DrawImage
// method, which takes our ImageAttributes structure as an argument.
// The ImageAttributes structure holds the value of the color that is
// to be "rendered" as transparent.
graphicsObject.DrawImage
(
image, // The image we created.
destRect, // Where to blit the image
0, 0, // The source rect's X and Y
image.Width, // The source rect's width...
image.Height, // ...and height
GraphicsUnit.Pixel, // The unit of measurement to use
ImageAttributes_Transparency // The ImageAttributes structure
// that we populated with information
// on which color to use as the
// transparent color.
);
}
}
///
/// This is called when the form is invalidated, and thus needs repainting.
///
private void Form1_Paint(object sender, PaintEventArgs e)
{
// The rest of the form repaints itself, just re-render our image...
RenderImage();
}
}
}
Coming once I have access to an open FTP port:
Download Project (Visual Studio C# 2008)
