圖片上傳並轉成灰白圖片

來源:互聯網
上載者:User

這是以前做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();
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.