首页 > 代码库 > web输出图片

web输出图片

1、已存在图片 WriteFile

string pathimg = AppDomain.CurrentDomain.BaseDirectory + "images\\20170424081926.png";

        Response.ContentType = "image/png";
        Response.WriteFile(pathimg);

 

2、已存在图片转成字节输出

using (FileStream fs = new FileStream(pathimg, FileMode.Open))
        {
            long le = fs.Length;
            byte[] arrImgNew = new byte[le];
            fs.Read(arrImgNew, 0, arrImgNew.Length);
            Response.ContentType = "image/Jpeg";
            HttpContext.Current.Response.BinaryWrite(arrImgNew);
        }

 

3、创建图片save到内存流输出

        string checkCode = "heihei";
        int iwidth = checkCode.Length * 24;
        Bitmap image = new Bitmap(iwidth, 50);
        Graphics g = Graphics.FromImage(image);
        g.Clear(Color.White);

        //定义颜色
        Color[] colors = { Color.Pink, Color.HotPink, Color.Green, Color.GreenYellow, Color.Black, Color.Red, Color.DarkBlue, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
        //定义字体
        string[] fonts = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体", "Microsoft yahei" };

        //随机输出噪点
        Random ran = new Random();
        for (int i = 0; i < 999; i++)
        {
            int x = ran.Next(image.Width);
            int y = ran.Next(image.Height);
            g.DrawRectangle(new Pen(Color.LightGray, 1), x, y, 1, 1);
        }

        //输出不同字体和颜色的验证码字符
        for (int i = 0; i < checkCode.Length; i++)
        {
            int cindex = ran.Next(10);
            int findex = ran.Next(5);

            Font f = new Font(fonts[findex], 28, FontStyle.Bold);
            Brush b = new SolidBrush(colors[cindex]);

            g.DrawString(checkCode.Substring(i, 1), f, b, (i * 20) + 8, 1);
        }
        //g.DrawRectangle(new Pen(Color.Black, 1), 0, 0, image.Width - 1, image.Height - 1);

        //输出到浏览器
        MemoryStream ms = new MemoryStream();
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        Response.ClearContent();
        Response.ContentType = "image/Jpeg";
        Response.BinaryWrite(ms.ToArray());
        g.Dispose();
        image.Dispose();

 

4、创建图片保存到二进制的输出流里Response.OutputStream

        using (System.Drawing.Image img = new Bitmap(220, 80))
        {
            using (Graphics g = Graphics.FromImage(img))
            {
                g.FillRectangle(Brushes.White, 0, 0, img.Width, img.Height);
                g.DrawRectangle(Pens.Gray, 0, 0, img.Width - 1, img.Height - 1);
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.DarkBlue, 1.2f, true);
                g.DrawString("ddd", new Font("微软雅黑", 18, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)), brush, 10, 10);
                Response.ContentType = "image/jpeg";
                img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }

 

web输出图片