This method refers to the method of drawing a 8-bit grayscale gif picture on MSDN.
Function 1,2 A custom color picture method for painting.
Function 3 (Getcolorpalette) and function 4 (Savegifwithnewgrayscale) are the MSDN original methods.
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Drawing;
Using System.Drawing.Imaging;
Namespace Em_picture {class Class2 {public static list<color> cl;
Function 1://This is original, reprint please Garchin connect (http://www.cnblogs.com/wdh1983/admin/EditPosts.aspx?postid=1378468)!
static void Drawpicture () {Image image = Image.FromFile ("Load your picture: For example, a total of 11-color stock market trend, its white background color");
Get 11 hue corresponding palette ColorPalette pal = Getcolorpalette (11);
Lists the colors contained in the picture cl = new list<color> (); Cl.
ADD (Color.FromArgb (255, 0, 0, 0)); Cl.
ADD (Color.FromArgb (255, 255, 0, 0)); Cl.
ADD (Color.FromArgb (255, 51, 51, 102)); Cl.
ADD (Color.FromArgb (255, 102, 102, 102)); Cl.
ADD (Color.FromArgb (255, 0, 128, 0)); Cl.
ADD (Color.FromArgb (255, 128, 128, 128)); Cl.
ADD (Color.FromArgb (255, 206, 158, 0)); Cl.
ADD (Color.FromArgb (255, 200, 200, 200)); Cl.
ADD (Color.FromArgb (255, 206, 206, 206)); Cl.
ADD (Color.FromArgb (255, 206, 207, 156)); Cl.
ADD (Color.FromArgb (255, 255, 255, 255)); Overrides the palette before 11 color for (int i = 0; i < CL. Count; i++) {pal.
Entries[i] = Cl[i]; } pal. ENTRIES[10] = Color.FromArgb (0, 255, 255, 255);
Modify its alpha value to 0 to make the background transparent//call draw a 8-bit color graph function savegifwithnewcolortable (image, pal, "You want to save the location of the new picture");
///Function 2://This is original, reprint please Garchin connect (http://www.cnblogs.com/wdh1983/admin/EditPosts.aspx?postid=1378468)! public static void savegifwithnewcolortable (image image, ColorPalette Pal, string filename) {int Widt H = image.
Width; int Height = image.
Height;
Bitmap Bitmap = new Bitmap (Width, Height, pixelformat.format8bppindexed); Bitmap.
Palette = pal; 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;
BitmapData bmdata = bmpcopy.lockbits (rect, Imagelockmode.readwrite, PIXELFORMAT.FORMAT32BPPARGB);
unsafe {byte* pbits; if (Bitmapdata.stride > 0) pbits = (byte*) pixels.
ToPointer (); else Pbits = (byte*) pixels.
ToPointer () + bitmapdata.stride * (Height-1);
UINT STRIDE = (UINT) math.abs (bitmapdata.stride);
//byte* p = (byte*) bmdata.scan0;
int offset = bmdata.stride-width * 4; for (uint row = 0; row < Height; ++row) {for (uint col = 0; col < Width; ++col) {///Take a point color value in the original pixel = Color.FromArgb (p[3
], p[2], p[1], p[0]);
p = 4;
Gets the pointer to the corresponding point of the new graph byte* P8bpppixel = pbits + row * Stride + col; Assigns the color index value corresponding to the color of this point of the original image to the pointer *p8bpppixel = (byte) cl.
IndexOf (pixel); ===========================================================//In fact, the above code compared to the consumption performance, the following code is better (also use the switch letter
Number)/* byte B = 0;
The Order of judgement is to establish the reverse of the color table//Because white is the background color, the majority of the judgment here will stop if (p[1] = = 255) b = 10; else if (p[1] = = 207) b = 9;
else if (p[1] = = 206) b = 8;
else if (p[1] = = 7;
else if (p[1] = = 158) b = 6;
else if (p[1] = = 128 && p[2] = = 128) b = 5;
else if (p[1] = = 128 && p[2] = = 0) b = 4;
else if (p[1] = = 102) b = 3;
else if (p[1] = =, B = 2;
else if (p[1] = = 0 && p[2] = = 1;
else if (p[1] = = 0 && p[2] = = 0) b = 0;
p = 4;
byte* P8bpppixel = pbits + row * Stride + col;
*p8bpppixel = b; *///===========================================================}/* End loop
For col */p = offset; }/* End loop for row */BMPCopy.unlockbits (Bmdata); }/* End unsafe * * bitmap.
Unlockbits (BitmapData); Bitmap.
Save (filename, imageformat.gif);
Bmpcopy.dispose (); Bitmap.
Dispose (); ///Function 3: Get the color palette//This is original, reprint please Garchin connect (http://www.cnblogs.com/wdh1983/admin/EditPosts.aspx?postid=1
378468)!
public static ColorPalette Getcolorpalette (UINT ncolors) {//Assume monochrome image.
PixelFormat bitscolordepth = pixelformat.format1bppindexed; ColorPalette Palette; The Palette we are stealing Bitmap Bitmap;
The source of the Stolen palette//determine number of colors.
if (Ncolors > 2) bitscolordepth = pixelformat.format4bppindexed;
if (ncolors >) bitscolordepth = pixelformat.format8bppindexed;
Make a new Bitmap the object to get its Palette. Bitmap= new Bitmap (1, 1, bitscolordepth); palette = Bitmap. Palette; Grab the palette bitmap. Dispose (); Cleanup the source Bitmap return palette; Send the Palette Back}//function 4: Draw 8-bit grayscale gif//This is original, reprint please Garchin (http://www.cnblogs.com/wdh1983/admin/Edi
tposts.aspx?postid=1378468)!
public static void Savegifwithnewgrayscale (image image, string filename, uint ncolors, bool ftransparent) {
if (Ncolors > 256) ncolors = 256;
if (Ncolors < 2) ncolors = 2; int Width = image.
Width; int Height = image.
Height;
Bitmap Bitmap = new Bitmap (Width, Height, pixelformat.format8bppindexed);
ColorPalette pal = Getcolorpalette (ncolors); for (UINT i = 0; i < ncolors; i++) {uint Alpha = 0xFF;
Colors are opaque. UINT intensity = i * 0xFF/(NCOLORS-1);
Even distribution. if (i = = 0 && ftransparent)//Make this color index Alpha = 0; Transparent pal.
Entries[i] = color.fromargb (int) Alpha, (int) intensity, (int) intensity, (int) intensity); } bitmap.
Palette = pal;
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;
unsafe {Byte* pbits; if (Bitmapdata.stride > 0) pbits = (byte*) pixels.
ToPointer (); else Pbits = (byte*) pixels.
ToPointer () + bitmapdata.stride * (Height-1);
UINT STRIDE = (UINT) math.abs (bitmapdata.stride);
for (uint row = 0; row < Height; ++row) {for (uint col = 0; col < Width; ++col) {Color pixel;
The source pixel.
byte* P8bpppixel = pbits + row * Stride + col;
Pixel = bmpcopy.getpixel (int) col, (int) row); Double luminance = (pixel. R * 0.299) + (pixel. G * 0.587) + (pixel.
B * 0.114);
*p8bpppixel = (byte) (Luminance * (nColors-1)/255 + 0.5); }/* End loop for col * * * * end loop for row/}/* End unsafe * * * bitmap. Unlockbits (Bitmapdata); Bitmap.
Save (filename, imageformat.gif);
Bmpcopy.dispose (); Bitmap.
Dispose (); }
}
}