Viewing source for recipe2212cs.aspx

<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Runtime.InteropServices" %>
<script language="C#" runat="server">
private ColorPalette GetColorPalette()
{
	Bitmap bitmap = new Bitmap(1, 1, PixelFormat.Format8bppIndexed);
	ColorPalette palette = bitmap.Palette;
	bitmap.Dispose();
	
	return palette;
}

private Bitmap ReColorGif(System.Drawing.Image image)
{
	int colors = 255;
	int width = image.Width;
	int height = image.Height;

	Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
	ColorPalette palette = GetColorPalette();

	SortedList oleColors = new SortedList();
	int[] safeColors = new int[] { 0, 51, 102, 153, 204, 255 };

    int count = 0;    
	for (int r = 0; r < safeColors.Length; r++)
	{
		for (int g = 0; g < safeColors.Length; g++)
		{
			for (int b = 0; b < safeColors.Length; b++)
			{
				palette.Entries[count] = Color.FromArgb(255, safeColors[r], safeColors[g], safeColors[b]);
				oleColors[ ColorTranslator.ToOle( palette.Entries[count] ) ] = count;
				count++;
			}
		}
	}
	palette.Entries[colors - 1] = Color.FromArgb(0, 255, 255, 255);

	bitmap.Palette = palette;
	Bitmap bmpCopy = new Bitmap(width, height, PixelFormat.Format32bppArgb);
	{
		Graphics g = Graphics.FromImage(bmpCopy);
		g.PageUnit = GraphicsUnit.Pixel;
		g.DrawImage(image, 0, 0, width, height);
		g.Dispose();
	}

	BitmapData bitmapData;
	Rectangle rect = new Rectangle(0, 0, width, height);
	bitmapData = bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
	IntPtr pixels = bitmapData.Scan0;

	int pBits;
	if (bitmapData.Stride > 0)
		pBits = pixels.ToInt32();
	else
		pBits = pixels.ToInt32() + bitmapData.Stride * (height - 1);

	int stride = Math.Abs(bitmapData.Stride);
	byte[] bits = new byte[height * stride];	
	
	for (int row = 0; row < height; ++row)
	{
		for (int col = 0; col < width; ++col)
		{	  
			int n8BppPixel = row * stride + col;
			Color pixel = bmpCopy.GetPixel(col, row);			  
			double index;
			
            if (ColorTranslator.ToHtml(pixel) == "#ffffff")
                index = colors - 1;
            else if (oleColors.ContainsKey( ColorTranslator.ToOle(pixel) ))
                index = Convert.ToDouble( oleColors[ ColorTranslator.ToOle(pixel) ] );
            else
            {
                int red, green, blue, entry = 0;
                while (safeColors[entry] < pixel.R)
                    entry++;
                red = safeColors[entry];
                entry = 0;

                while (safeColors[entry] < pixel.G)
                    entry++;
                green = safeColors[entry];
                entry = 0;

                while (safeColors[entry] < pixel.B)
                    entry++;
                blue = safeColors[entry];
                entry = 0;

				index = Convert.ToDouble( oleColors[ColorTranslator.ToOle( Color.FromArgb(red, green, blue) )] );
			}

			bits[n8BppPixel] = Convert.ToByte(index);
		}
	}	
	
	CopyArrayTo(pBits, bits, bits.Length);
	bitmap.UnlockBits(bitmapData);
	
	return bitmap;

	bmpCopy.Dispose();
	bitmap.Dispose();
}

[DllImport("KERNEL32.DLL", EntryPoint="RtlMoveMemory", SetLastError=true, CharSet=CharSet.Auto, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
public static extern void CopyArrayTo([In, MarshalAs(UnmanagedType.I4)] Int32 hpvDest, [In, Out] byte[] hpvSource, int cbCopy);

private void Page_Load(object sender, System.EventArgs e)
{
	Bitmap outputBitmap = new Bitmap(Server.MapPath("Recipe2212.jpg"));
	
	outputBitmap = ReColorGif(outputBitmap);

	Response.ContentType = "image/gif";
    outputBitmap.Save(Response.OutputStream, ImageFormat.Gif);

    outputBitmap.Dispose();
}
</script>