zoukankan      html  css  js  c++  java
  • C#读取数据库图片显示、缩小、更新

    ashx源文件代码:

    <%@ WebHandler Language="C#" Class="GetThumbnailImageByEmpCode" %>
    
    using System;
    using System.Web;
    using System.Data.SqlClient;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.IO;
    using System.Drawing.Imaging;
    
    public class GetThumbnailImageByEmpCode : IHttpHandler
    {
    
        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.QueryString["code"] == null)
            {
                return;
            }
            int newWidth = 336;         //目标图片宽度
            int newHeight = 252;        //目标图片高度
            bool bNeedUpdate = false;   //是否需要更新数据库
            System.Drawing.Image Img;
            using (SqlConnection conn = new SqlConnection(db.ConnectionStrings.hrConnectionString))
            using (SqlCommand cmd = new SqlCommand("", conn))
            {
                cmd.CommandText = "select photo from ZlEmployee where code=@code and DATALENGTH(photo)>1000";
                cmd.Parameters.AddWithValue("code", context.Request.QueryString["code"]);
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    byte[] imgData = (byte[])reader[0];
                    reader.Close();
    
                    using (MemoryStream ms = new MemoryStream(imgData))
                    {
                        Img = System.Drawing.Image.FromStream(ms);
                    }
                    
                    if (Img.Width > newWidth || Img.Height > newHeight)
                    {
                        Img = reSizeImage(Img);
                        bNeedUpdate = true;
                    }
    
                    using (MemoryStream ms = new MemoryStream())
                    {
                        #region 输出图片到浏览器
                        Bitmap jpg = new Bitmap(Img);
                        jpg.Save(ms, ImageFormat.Jpeg);
                        context.Response.Clear();
                        context.Response.Charset = "utf-8";
                        context.Response.ContentType = "image/pjpeg";
                        context.Response.BinaryWrite(ms.ToArray());
                        #endregion
                        //检查是否需要更新
                        if (bNeedUpdate)
                        {
                            cmd.CommandText = "update ZlEmployee set photo=@photo where code=@code";
                            cmd.Parameters.Clear();
                            cmd.Parameters.AddWithValue("code", context.Request.QueryString["code"]);
                            cmd.Parameters.AddWithValue("photo", ms.ToArray());
                            cmd.ExecuteNonQuery();
                        }
                    }
                    context.Response.End();
                }
            }
        }
    
        public System.Drawing.Image reSizeImage(System.Drawing.Image img)
        {
            int newWidth = 336;
            int newHeight = 252;
    
            //如果是高度大于宽度则按竖图处理,宽小于高
            if (img.Height > img.Width)
            {
                newWidth = 252;
                newHeight = 336;
            }
    
            #region 计算按比例缩放的图片尺寸
            if (img.Height / img.Width > newHeight / newWidth)
            {
                newWidth = newHeight * img.Width / img.Height;
            }
            else
            {
                newHeight = img.Height * newWidth / img.Width;
            }
            #endregion
    
            #region 缩放图片
            using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap((int)newWidth, (int)newHeight, PixelFormat.Format32bppArgb)) //using   3 
            {
                using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap))
                {
                    //清除整个绘图面并以透明背景色填充 
                    graphics.Clear(Color.Transparent);
                    //在指定位置并且按指定大小绘制   原图片   对象 
                    graphics.DrawImage(img, new Rectangle(0, 0, (int)newWidth, (int)newHeight));
                    using (MemoryStream ms = new MemoryStream())
                    {
                        bitmap.Save(ms, ImageFormat.Jpeg);
                        return System.Drawing.Image.FromStream(ms);
                    }
                }
            }
            #endregion
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }


  • 相关阅读:
    HDU 4705 Y
    POJ 3614 Sunscreen
    Aizu 2170 Marked Ancestor
    POJ 3616 Milking Time
    POJ 2385 Apple Catching
    POJ 2229 Sunsets
    HDU 4678 Mine
    树的重量
    579Div3
    迷途之家2019联赛
  • 原文地址:https://www.cnblogs.com/apollokk/p/6713926.html
Copyright © 2011-2022 走看看