图片上传并转成灰白图片

这是以前做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[
00= 1 / 3f;
                _matrix[
01= 1 / 3f;
                _matrix[
02= 1 / 3f;
                _matrix[
10= 1 / 3f;
                _matrix[
11= 1 / 3f;
                _matrix[
12= 1 / 3f;
                _matrix[
20= 1 / 3f;
                _matrix[
21= 1 / 3f;
                _matrix[
22= 1 / 3f;
                ImageAttributes _attributes 
= new ImageAttributes();
                _attributes.SetColorMatrix(_matrix);
                MapGraphy.DrawImage(ImageDemo, OutputArea, 
00, 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();
        }

你可能感兴趣的:(图片上传并转成灰白图片)