web輸出圖片

來源:互聯網
上載者:User

標籤:nbsp   save   draw   write   from   open   for   color   check   

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輸出圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.