最近需要图像保存到配置文件中,使用了以下的代码把图像转换成byte[]
由于需要的图片都很小,直接存成bmp格式了。但是遇到透明背景的图像时,保存下来的图像背景为黑色了。目前使用解决的办法是把图像保存为GIF格式
参考文章:
http://www.microsoft.com/china/MSDN/library/archives/library/DNAspp/html/colorquant.asp
http://support.microsoft.com/kb/319061/
http://blog.csdn.net/ncjmc/archive/2006/08/28/1132879.aspx
基本保留了 Morgan Skinner的代码,进行了少量的修改,使用调色板的方式完成了保存gif功能,保存的图像有少量失真。
完全代码如下:
其中相对于
Morgan Skinner的代码,只添加了Get8bppColorPalette这个函数,修改了PaletteQuantizer类的构造函数
现在代码更新为
private
byte
[] GetByteFromImage(Image inputimage)
{
byte[] byteImage = new byte[0];
using (MemoryStream ms = new MemoryStream())
{
try
{
Bitmap bitmap = new Bitmap(inputimage);
Rectangle rect = new Rectangle(0,0, inputimageWidth, inputimage.Height);
System.Drawing.Imaging.BitmapData bd = bitmap.LockBits(rect,
System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byteImage = ms.ToArray();
bitmap.UnlockBits(bd);
}
catch (Exception ex)
{
//Error
}
}
return byteImage;
}
{
byte[] byteImage = new byte[0];
using (MemoryStream ms = new MemoryStream())
{
try
{
Bitmap bitmap = new Bitmap(inputimage);
Rectangle rect = new Rectangle(0,0, inputimageWidth, inputimage.Height);
System.Drawing.Imaging.BitmapData bd = bitmap.LockBits(rect,
System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byteImage = ms.ToArray();
bitmap.UnlockBits(bd);
}
catch (Exception ex)
{
//Error
}
}
return byteImage;
}
参考文章:
http://www.microsoft.com/china/MSDN/library/archives/library/DNAspp/html/colorquant.asp
http://support.microsoft.com/kb/319061/
http://blog.csdn.net/ncjmc/archive/2006/08/28/1132879.aspx
基本保留了 Morgan Skinner的代码,进行了少量的修改,使用调色板的方式完成了保存gif功能,保存的图像有少量失真。
完全代码如下:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace Quantizer
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public unsafe abstract class Quantizer
{
/// <summary>
/// Construct the quantizer
/// </summary>
/// <param name="singlePass">If true, the quantization only needs to loop through the source pixels once</param>
/// <remarks>
/// If you construct this class with a true value for singlePass, then the code will, when quantizing your image,
/// only call the 'QuantizeImage' function. If two passes are required, the code will call 'InitialQuantizeImage'
/// and then 'QuantizeImage'.
/// </remarks>
public Quantizer(bool singlePass)
{
_singlePass = singlePass;
}
/// <summary>
/// Quantize an image and return the resulting output bitmap
/// </summary>
/// <param name="source">The image to quantize</param>
/// <returns>A quantized version of the image</returns>
public Bitmap Quantize(Image source)
{
// Get the size of the source image
int height = source.Height;
int width = source.Width;
// And construct a rectangle from these dimensions
Rectangle bounds = new Rectangle(0, 0, width, height);
// First off take a 32bpp copy of the image
Bitmap copy = new Bitmap(width, height, PixelFormat.Format32bppArgb);
// And construct an 8bpp version
Bitmap output = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
// Now lock the bitmap into memory
using (Graphics g = Graphics.FromImage(copy))
{
g.PageUnit = GraphicsUnit.Pixel;
// Draw the source image onto the copy bitmap,
// which will effect a widening as appropriate.
g.DrawImageUnscaled(source, bounds);
}
// Define a pointer to the bitmap data
BitmapData sourceData = null;
try
{
// Get the source image bits and lock into memory
sourceData = copy.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
// Call the FirstPass function if not a single pass algorithm.
// For something like an octree quantizer, this will run through
// all image pixels, build a data structure, and create a palette.
if (!_singlePass)
FirstPass(sourceData, width, height);
// Then set the color palette on the output bitmap. I'm passing in the current palette
// as there's no way to construct a new, empty palette.
output.Palette = this.GetPalette(output.Palette);
// Then call the second pass which actually does the conversion
SecondPass(sourceData, output, width, height, bounds);
}
finally
{
// Ensure that the bits are unlocked
copy.UnlockBits(sourceData);
}
// Last but not least, return the output bitmap
return output;
}
/// <summary>
/// Execute the first pass through the pixels in the image
/// </summary>
/// <param name="sourceData">The source data</param>
/// <param name="width">The width in pixels of the image</param>
/// <param name="height">The height in pixels of the image</param>
protected virtual void FirstPass(BitmapData sourceData, int width, int height)
{
// Define the source data pointers. The source row is a byte to
// keep addition of the stride value easier (as this is in bytes)
byte* pSourceRow = (byte*)sourceData.Scan0.ToPointer();
Int32* pSourcePixel;
// Loop through each row
for (int row = 0; row < height; row++)
{
// Set the source pixel to the first pixel in this row
pSourcePixel = (Int32*)pSourceRow;
// And loop through each column
for (int col = 0; col < width; col++, pSourcePixel++)
// Now I have the pixel, call the FirstPassQuantize function...
InitialQuantizePixel((Color32*)pSourcePixel);
// Add the stride to the source row
pSourceRow += sourceData.Stride;
}
}
/// <summary>
/// Execute a second pass through the bitmap
/// </summary>
/// <param name="sourceData">The source bitmap, locked into memory</param>
/// <param name="output">The output bitmap</param>
/// <param name="width">The width in pixels of the image</param>
/// <param name="height">The height in pixels of the image</param>
/// <param name="bounds">The bounding rectangle</param>
protected virtual void SecondPass(BitmapData sourceData, Bitmap output, int width, int height, Rectangle bounds)
{
BitmapData outputData = null;
try
{
// Lock the output bitmap into memory
outputData = output.LockBits(bounds, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
// Define the source data pointers. The source row is a byte to
// keep addition of the stride value easier (as this is in bytes)
byte* pSourceRow = (byte*)sourceData.Scan0.ToPointer();
Int32* pSourcePixel = (Int32*)pSourceRow;
Int32* pPreviousPixel = pSourcePixel;
// Now define the destination data pointers
byte* pDestinationRow = (byte*)outputData.Scan0.ToPointer();
byte* pDestinationPixel = pDestinationRow;
// And convert the first pixel, so that I have values going into the loop
byte pixelValue = QuantizePixel((Color32*)pSourcePixel);
// Assign the value of the first pixel
*pDestinationPixel = pixelValue;
// Loop through each row
for (int row = 0; row < height; row++)
{
// Set the source pixel to the first pixel in this row
pSourcePixel = (Int32*)pSourceRow;
// And set the destination pixel pointer to the first pixel in the row
pDestinationPixel = pDestinationRow;
// Loop through each pixel on this scan line
for (int col = 0; col < width; col++, pSourcePixel++, pDestinationPixel++)
{
// Check if this is the same as the last pixel. If so use that value
// rather than calculating it again. This is an inexpensive optimisation.
if (*pPreviousPixel != *pSourcePixel)
{
// Quantize the pixel
pixelValue = QuantizePixel((Color32*)pSourcePixel);
// And setup the previous pointer
pPreviousPixel = pSourcePixel;
}
// And set the pixel in the output
*pDestinationPixel = pixelValue;
}
// Add the stride to the source row
pSourceRow += sourceData.Stride;
// And to the destination row
pDestinationRow += outputData.Stride;
}
}
finally
{
// Ensure that I unlock the output bits
output.UnlockBits(outputData);
}
}
/// <summary>
/// Override this to process the pixel in the first pass of the algorithm
/// </summary>
/// <param name="pixel">The pixel to quantize</param>
/// <remarks>
/// This function need only be overridden if your quantize algorithm needs two passes,
/// such as an Octree quantizer.
/// </remarks>
protected virtual void InitialQuantizePixel(Color32* pixel)
{
}
/// <summary>
/// Override this to process the pixel in the second pass of the algorithm
/// </summary>
/// <param name="pixel">The pixel to quantize</param>
/// <returns>The quantized value</returns>
protected abstract byte QuantizePixel(Color32* pixel);
/// <summary>
/// Retrieve the palette for the quantized image
/// </summary>
/// <param name="original">Any old palette, this is overrwritten</param>
/// <returns>The new color palette</returns>
protected abstract ColorPalette GetPalette(ColorPalette original);
/// <summary>
/// Flag used to indicate whether a single pass or two passes are needed for quantization.
/// </summary>
private bool _singlePass;
/// <summary>
/// Struct that defines a 32 bpp colour
/// </summary>
/// <remarks>
/// This struct is used to read data from a 32 bits per pixel image
/// in memory, and is ordered in this manner as this is the way that
/// the data is layed out in memory
/// </remarks>
[StructLayout(LayoutKind.Explicit)]
public struct Color32
{
/// <summary>
/// Holds the blue component of the colour
/// </summary>
[FieldOffset(0)]
public byte Blue;
/// <summary>
/// Holds the green component of the colour
/// </summary>
[FieldOffset(1)]
public byte Green;
/// <summary>
/// Holds the red component of the colour
/// </summary>
[FieldOffset(2)]
public byte Red;
/// <summary>
/// Holds the alpha component of the colour
/// </summary>
[FieldOffset(3)]
public byte Alpha;
/// <summary>
/// Permits the color32 to be treated as an int32
/// </summary>
[FieldOffset(0)]
public int ARGB;
/// <summary>
/// Return the color for this Color32 object
/// </summary>
public Color Color
{
get { return Color.FromArgb(Alpha, Red, Green, Blue); }
}
}
}
/// <summary>
/// Summary description for PaletteQuantizer.
/// </summary>
public unsafe class PaletteQuantizer : Quantizer
{
/// <summary>
/// Construct the palette quantizer
/// </summary>
/// <param name="palette">The color palette to quantize to</param>
/// <remarks>
/// Palette quantization only requires a single quantization step
/// </remarks>
public PaletteQuantizer()
: base(true)
{
ColorPalette palette = Get8bppColorPalette();
_colorMap = new Hashtable();
_colors = palette.Entries;
}
/// <summary>
/// Get a Format8bppIndexed type ColorPalette
/// </summary>
/// <returns></returns>
private ColorPalette Get8bppColorPalette()
{
// 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.
bitscolordepth = PixelFormat.Format8bppIndexed;
// Make a new Bitmap 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
}
/// <summary>
/// Override this to process the pixel in the second pass of the algorithm
/// </summary>
/// <param name="pixel">The pixel to quantize</param>
/// <returns>The quantized value</returns>
protected override byte QuantizePixel(Color32* pixel)
{
byte colorIndex = 0;
int colorHash = pixel->ARGB;
// Check if the color is in the lookup table
if (_colorMap.ContainsKey(colorHash))
colorIndex = (byte)_colorMap[colorHash];
else
{
// Not found - loop through the palette and find the nearest match.
// Firstly check the alpha value - if 0, lookup the transparent color
if (0 == pixel->Alpha)
{
// Transparent. Lookup the first color with an alpha value of 0
for (int index = 0; index < _colors.Length; index++)
{
if (0 == _colors[index].A)
{
colorIndex = (byte)index;
break;
}
}
}
else
{
// Not transparent...
int leastDistance = int.MaxValue;
int red = pixel->Red;
int green = pixel->Green;
int blue = pixel->Blue;
// Loop through the entire palette, looking for the closest color match
for (int index = 0; index < _colors.Length; index++)
{
Color paletteColor = _colors[index];
int redDistance = paletteColor.R - red;
int greenDistance = paletteColor.G - green;
int blueDistance = paletteColor.B - blue;
int distance = (redDistance * redDistance) +
(greenDistance * greenDistance) +
(blueDistance * blueDistance);
if (distance < leastDistance)
{
colorIndex = (byte)index;
leastDistance = distance;
// And if it's an exact match, exit the loop
if (0 == distance)
break;
}
}
}
// Now I have the color, pop it into the hashtable for next time
_colorMap.Add(colorHash, colorIndex);
}
return colorIndex;
}
/// <summary>
/// Retrieve the palette for the quantized image
/// </summary>
/// <param name="palette">Any old palette, this is overrwritten</param>
/// <returns>The new color palette</returns>
protected override ColorPalette GetPalette(ColorPalette palette)
{
for (int index = 0; index < _colors.Length; index++)
palette.Entries[index] = _colors[index];
return palette;
}
/// <summary>
/// Lookup table for colors
/// </summary>
private Hashtable _colorMap;
/// <summary>
/// List of all colors in the palette
/// </summary>
private Color[] _colors;
}
}
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace Quantizer
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public unsafe abstract class Quantizer
{
/// <summary>
/// Construct the quantizer
/// </summary>
/// <param name="singlePass">If true, the quantization only needs to loop through the source pixels once</param>
/// <remarks>
/// If you construct this class with a true value for singlePass, then the code will, when quantizing your image,
/// only call the 'QuantizeImage' function. If two passes are required, the code will call 'InitialQuantizeImage'
/// and then 'QuantizeImage'.
/// </remarks>
public Quantizer(bool singlePass)
{
_singlePass = singlePass;
}
/// <summary>
/// Quantize an image and return the resulting output bitmap
/// </summary>
/// <param name="source">The image to quantize</param>
/// <returns>A quantized version of the image</returns>
public Bitmap Quantize(Image source)
{
// Get the size of the source image
int height = source.Height;
int width = source.Width;
// And construct a rectangle from these dimensions
Rectangle bounds = new Rectangle(0, 0, width, height);
// First off take a 32bpp copy of the image
Bitmap copy = new Bitmap(width, height, PixelFormat.Format32bppArgb);
// And construct an 8bpp version
Bitmap output = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
// Now lock the bitmap into memory
using (Graphics g = Graphics.FromImage(copy))
{
g.PageUnit = GraphicsUnit.Pixel;
// Draw the source image onto the copy bitmap,
// which will effect a widening as appropriate.
g.DrawImageUnscaled(source, bounds);
}
// Define a pointer to the bitmap data
BitmapData sourceData = null;
try
{
// Get the source image bits and lock into memory
sourceData = copy.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
// Call the FirstPass function if not a single pass algorithm.
// For something like an octree quantizer, this will run through
// all image pixels, build a data structure, and create a palette.
if (!_singlePass)
FirstPass(sourceData, width, height);
// Then set the color palette on the output bitmap. I'm passing in the current palette
// as there's no way to construct a new, empty palette.
output.Palette = this.GetPalette(output.Palette);
// Then call the second pass which actually does the conversion
SecondPass(sourceData, output, width, height, bounds);
}
finally
{
// Ensure that the bits are unlocked
copy.UnlockBits(sourceData);
}
// Last but not least, return the output bitmap
return output;
}
/// <summary>
/// Execute the first pass through the pixels in the image
/// </summary>
/// <param name="sourceData">The source data</param>
/// <param name="width">The width in pixels of the image</param>
/// <param name="height">The height in pixels of the image</param>
protected virtual void FirstPass(BitmapData sourceData, int width, int height)
{
// Define the source data pointers. The source row is a byte to
// keep addition of the stride value easier (as this is in bytes)
byte* pSourceRow = (byte*)sourceData.Scan0.ToPointer();
Int32* pSourcePixel;
// Loop through each row
for (int row = 0; row < height; row++)
{
// Set the source pixel to the first pixel in this row
pSourcePixel = (Int32*)pSourceRow;
// And loop through each column
for (int col = 0; col < width; col++, pSourcePixel++)
// Now I have the pixel, call the FirstPassQuantize function...
InitialQuantizePixel((Color32*)pSourcePixel);
// Add the stride to the source row
pSourceRow += sourceData.Stride;
}
}
/// <summary>
/// Execute a second pass through the bitmap
/// </summary>
/// <param name="sourceData">The source bitmap, locked into memory</param>
/// <param name="output">The output bitmap</param>
/// <param name="width">The width in pixels of the image</param>
/// <param name="height">The height in pixels of the image</param>
/// <param name="bounds">The bounding rectangle</param>
protected virtual void SecondPass(BitmapData sourceData, Bitmap output, int width, int height, Rectangle bounds)
{
BitmapData outputData = null;
try
{
// Lock the output bitmap into memory
outputData = output.LockBits(bounds, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
// Define the source data pointers. The source row is a byte to
// keep addition of the stride value easier (as this is in bytes)
byte* pSourceRow = (byte*)sourceData.Scan0.ToPointer();
Int32* pSourcePixel = (Int32*)pSourceRow;
Int32* pPreviousPixel = pSourcePixel;
// Now define the destination data pointers
byte* pDestinationRow = (byte*)outputData.Scan0.ToPointer();
byte* pDestinationPixel = pDestinationRow;
// And convert the first pixel, so that I have values going into the loop
byte pixelValue = QuantizePixel((Color32*)pSourcePixel);
// Assign the value of the first pixel
*pDestinationPixel = pixelValue;
// Loop through each row
for (int row = 0; row < height; row++)
{
// Set the source pixel to the first pixel in this row
pSourcePixel = (Int32*)pSourceRow;
// And set the destination pixel pointer to the first pixel in the row
pDestinationPixel = pDestinationRow;
// Loop through each pixel on this scan line
for (int col = 0; col < width; col++, pSourcePixel++, pDestinationPixel++)
{
// Check if this is the same as the last pixel. If so use that value
// rather than calculating it again. This is an inexpensive optimisation.
if (*pPreviousPixel != *pSourcePixel)
{
// Quantize the pixel
pixelValue = QuantizePixel((Color32*)pSourcePixel);
// And setup the previous pointer
pPreviousPixel = pSourcePixel;
}
// And set the pixel in the output
*pDestinationPixel = pixelValue;
}
// Add the stride to the source row
pSourceRow += sourceData.Stride;
// And to the destination row
pDestinationRow += outputData.Stride;
}
}
finally
{
// Ensure that I unlock the output bits
output.UnlockBits(outputData);
}
}
/// <summary>
/// Override this to process the pixel in the first pass of the algorithm
/// </summary>
/// <param name="pixel">The pixel to quantize</param>
/// <remarks>
/// This function need only be overridden if your quantize algorithm needs two passes,
/// such as an Octree quantizer.
/// </remarks>
protected virtual void InitialQuantizePixel(Color32* pixel)
{
}
/// <summary>
/// Override this to process the pixel in the second pass of the algorithm
/// </summary>
/// <param name="pixel">The pixel to quantize</param>
/// <returns>The quantized value</returns>
protected abstract byte QuantizePixel(Color32* pixel);
/// <summary>
/// Retrieve the palette for the quantized image
/// </summary>
/// <param name="original">Any old palette, this is overrwritten</param>
/// <returns>The new color palette</returns>
protected abstract ColorPalette GetPalette(ColorPalette original);
/// <summary>
/// Flag used to indicate whether a single pass or two passes are needed for quantization.
/// </summary>
private bool _singlePass;
/// <summary>
/// Struct that defines a 32 bpp colour
/// </summary>
/// <remarks>
/// This struct is used to read data from a 32 bits per pixel image
/// in memory, and is ordered in this manner as this is the way that
/// the data is layed out in memory
/// </remarks>
[StructLayout(LayoutKind.Explicit)]
public struct Color32
{
/// <summary>
/// Holds the blue component of the colour
/// </summary>
[FieldOffset(0)]
public byte Blue;
/// <summary>
/// Holds the green component of the colour
/// </summary>
[FieldOffset(1)]
public byte Green;
/// <summary>
/// Holds the red component of the colour
/// </summary>
[FieldOffset(2)]
public byte Red;
/// <summary>
/// Holds the alpha component of the colour
/// </summary>
[FieldOffset(3)]
public byte Alpha;
/// <summary>
/// Permits the color32 to be treated as an int32
/// </summary>
[FieldOffset(0)]
public int ARGB;
/// <summary>
/// Return the color for this Color32 object
/// </summary>
public Color Color
{
get { return Color.FromArgb(Alpha, Red, Green, Blue); }
}
}
}
/// <summary>
/// Summary description for PaletteQuantizer.
/// </summary>
public unsafe class PaletteQuantizer : Quantizer
{
/// <summary>
/// Construct the palette quantizer
/// </summary>
/// <param name="palette">The color palette to quantize to</param>
/// <remarks>
/// Palette quantization only requires a single quantization step
/// </remarks>
public PaletteQuantizer()
: base(true)
{
ColorPalette palette = Get8bppColorPalette();
_colorMap = new Hashtable();
_colors = palette.Entries;
}
/// <summary>
/// Get a Format8bppIndexed type ColorPalette
/// </summary>
/// <returns></returns>
private ColorPalette Get8bppColorPalette()
{
// 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.
bitscolordepth = PixelFormat.Format8bppIndexed;
// Make a new Bitmap 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
}
/// <summary>
/// Override this to process the pixel in the second pass of the algorithm
/// </summary>
/// <param name="pixel">The pixel to quantize</param>
/// <returns>The quantized value</returns>
protected override byte QuantizePixel(Color32* pixel)
{
byte colorIndex = 0;
int colorHash = pixel->ARGB;
// Check if the color is in the lookup table
if (_colorMap.ContainsKey(colorHash))
colorIndex = (byte)_colorMap[colorHash];
else
{
// Not found - loop through the palette and find the nearest match.
// Firstly check the alpha value - if 0, lookup the transparent color
if (0 == pixel->Alpha)
{
// Transparent. Lookup the first color with an alpha value of 0
for (int index = 0; index < _colors.Length; index++)
{
if (0 == _colors[index].A)
{
colorIndex = (byte)index;
break;
}
}
}
else
{
// Not transparent...
int leastDistance = int.MaxValue;
int red = pixel->Red;
int green = pixel->Green;
int blue = pixel->Blue;
// Loop through the entire palette, looking for the closest color match
for (int index = 0; index < _colors.Length; index++)
{
Color paletteColor = _colors[index];
int redDistance = paletteColor.R - red;
int greenDistance = paletteColor.G - green;
int blueDistance = paletteColor.B - blue;
int distance = (redDistance * redDistance) +
(greenDistance * greenDistance) +
(blueDistance * blueDistance);
if (distance < leastDistance)
{
colorIndex = (byte)index;
leastDistance = distance;
// And if it's an exact match, exit the loop
if (0 == distance)
break;
}
}
}
// Now I have the color, pop it into the hashtable for next time
_colorMap.Add(colorHash, colorIndex);
}
return colorIndex;
}
/// <summary>
/// Retrieve the palette for the quantized image
/// </summary>
/// <param name="palette">Any old palette, this is overrwritten</param>
/// <returns>The new color palette</returns>
protected override ColorPalette GetPalette(ColorPalette palette)
{
for (int index = 0; index < _colors.Length; index++)
palette.Entries[index] = _colors[index];
return palette;
}
/// <summary>
/// Lookup table for colors
/// </summary>
private Hashtable _colorMap;
/// <summary>
/// List of all colors in the palette
/// </summary>
private Color[] _colors;
}
}
现在代码更新为
private
byte
[] GetByteFromImage(Image inputimage)
{
byte[] byteImage = new byte[0];
using (MemoryStream ms = new MemoryStream())
{
try
{
Bitmap bitmap;
PaletteQuantizer quantize = new PaletteQuantizer();
bitmap = quantize.Quantize(inputimage);
quantize = null;
bitmap.Save(ms, ImageFormat.Gif);
byteImage = ms.ToArray();
}
catch (Exception ex)
{
//Error
}
}
GC.Collect();
return byteImage;
}
{
byte[] byteImage = new byte[0];
using (MemoryStream ms = new MemoryStream())
{
try
{
Bitmap bitmap;
PaletteQuantizer quantize = new PaletteQuantizer();
bitmap = quantize.Quantize(inputimage);
quantize = null;
bitmap.Save(ms, ImageFormat.Gif);
byteImage = ms.ToArray();
}
catch (Exception ex)
{
//Error
}
}
GC.Collect();
return byteImage;
}