这是以前做webim时写的上传用户头像时要生成离线状态的图片,即黑白图片的代码
函数代码如下:
Code
public void MakeSmallImg(string fileName, string saveImg, Rectangle OutputArea,bool toBlackWhite)
{
System.Drawing.Image ImageDemo = System.Drawing.Image.FromFile(fileName, true);
System.Drawing.Bitmap OutputImage = new System.Drawing.Bitmap(OutputArea.Width, OutputArea.Height);
System.Drawing.Graphics MapGraphy = System.Drawing.Graphics.FromImage(OutputImage);
MapGraphy.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
MapGraphy.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
MapGraphy.Clear(System.Drawing.Color.White);
if (toBlackWhite)
{
ColorMatrix _matrix = new ColorMatrix();
_matrix[0, 0] = 1 / 3f;
_matrix[0, 1] = 1 / 3f;
_matrix[0, 2] = 1 / 3f;
_matrix[1, 0] = 1 / 3f;
_matrix[1, 1] = 1 / 3f;
_matrix[1, 2] = 1 / 3f;
_matrix[2, 0] = 1 / 3f;
_matrix[2, 1] = 1 / 3f;
_matrix[2, 2] = 1 / 3f;
ImageAttributes _attributes = new ImageAttributes();
_attributes.SetColorMatrix(_matrix);
MapGraphy.DrawImage(ImageDemo, OutputArea, 0, 0, ImageDemo.Width, ImageDemo.Height, GraphicsUnit.Pixel, _attributes);
}
else
{
MapGraphy.DrawImage(ImageDemo, OutputArea);
}
OutputImage.Save(saveImg, System.Drawing.Imaging.ImageFormat.Bmp);
MapGraphy.Dispose();
OutputImage.Dispose();
ImageDemo.Dispose();
}