zoukankan      html  css  js  c++  java
  • C#将图片转化为黑白图片

    最近项目需要将上传的图片转化为黑白图片

    在网上找了很多资料,测试通过,上代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    
    namespace Test
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Bitmap b = new Bitmap(Server.MapPath("1.jpg"));
                for (int x = 0; x < b.Width; x++)
                {
                    for (int y = 0; y < b.Height; y++)
                    {
                        Color pixel = b.GetPixel(x, y);
                        int val = (pixel.R + pixel.G + pixel.B) / 3;
                        b.SetPixel(x, y, Color.FromArgb(val, val, val));
    
                    }
                }
                //b.Save(Server.MapPath("New1.jpg"));  //存储图片
    
                //直接显示图片
                MemoryStream ms = new MemoryStream();
                b.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                byte[] bytes = ms.GetBuffer();  //byte[]   bytes=   ms.ToArray(); 
    
                Response.ContentType = "image/Jpeg";
                Response.BinaryWrite(bytes);
                Response.End();
            }
        }
    }
  • 相关阅读:
    题解报告:hdu 2062 Subset sequence
    CSS3滑块菜单
    CSS3环形动画菜单
    可折叠显示的发光搜索表单
    Tab动画菜单
    侧边自定义滚动条
    css3条纹边框效果
    css3图片过滤效果
    CSS3图片悬停放大动画
    CSS3响应式侧边菜单
  • 原文地址:https://www.cnblogs.com/youmeng/p/3312576.html
Copyright © 2011-2022 走看看