C#读取数据库图片显示、缩小、更新
来源:互联网 发布:openwrt 进入webshell 编辑:程序博客网 时间:2024/05/16 18:57
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; } }}
- C#读取数据库图片显示、缩小、更新
- C#保存图片到数据库,读取图片显示
- C#从数据库中读取二进制数据,并显示图片
- c# 图片保存到数据库和从数据库读取图片并显示
- c# winform richtextbox将图片插入数据库、读取数据库显示图片
- C# 图片放大、缩小
- c#读取数据库中图片路径,将图片保存到imagelist,在listview显示
- C#将图片以二进制流形式存到数据库,并读取显示
- js比例缩小显示图片
- js比例缩小显示图片
- js比例缩小显示图片
- 将图片按比例缩小显示
- Android放大缩小显示图片
- .Net读取数据库中的图片并显示
- PowerBuilder从数据库读取并显示图片。
- Jsp从数据库读取并显示图片
- 读取数据库Image格式图片并显示
- grails从数据库读取blob显示图片
- DB2中的数据类型
- How to extract RPM or DEB packages
- 高德地图全解
- 数据模型
- 云计算
- C#读取数据库图片显示、缩小、更新
- Android Canvas Layers
- VS2010与.NET4系列 5.代码优化的Web开发轮廓
- 正确使用 volatile 的模式
- Flash AS3 垃圾回收机制详解
- C#读取图片Exif信息
- C++,重载输出操作符<<, cout.operator()是什么东东?
- map key
- 求第1500个能被2或者3或者5所整除的数