[英]How do I store a image BLOB in an access database after getting the name from openfiledialog?
[英]How do I resize an image after converting it from a BLOB?
我試圖在通用處理程序中調整圖像服務器端的大小,然后將其從數據庫中的BLOB轉換回圖像。這是我的處理程序代碼:
<%@ WebHandler Language="C#" Class="Image" %>
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.IO;
public class Image : IHttpHandler {
public void ProcessRequest (HttpContext context) {
Guid id = new Guid(context.Request.QueryString["Id"]);
int column = 7;
if (context.Request.QueryString["img"] == "tbn")
{
column = 6;
}
context.Response.ContentType = "image/png";
MemoryStream strm = new MemoryStream(returnImage(id, column));
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
}
public Byte[] returnImage(Guid id, int column)
{
SqlConnection sqlCn = new SqlConnection("Data Source=localhost;Initial Catalog=database;User ID=user;Password=password");
string qry = "SELECT * FROM Project WHERE Id=@id";
SqlCommand cmd = new SqlCommand(qry, sqlCn);
cmd.Parameters.Add("@id", SqlDbType.UniqueIdentifier).Value = id;
sqlCn.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
Byte[] ar = (Byte[])(dr[column]);
dr.Close();
cmd.Dispose();
sqlCn.Close();
return ar;
}
public bool IsReusable {
get {
return false;
}
}
}
當圖像寬度大於其高度時,它應該看起來相反;而根據寬度,它應該設置為高或寬,並且其他值(高度/寬度)應按比例設置,以免拉長。
我發現的是這樣的: http : //www.codeproject.com/Articles/25838/A-Simple-Image-Handler
但是我真的不知道怎么使用...有什么建議嗎? 感謝您提前提供的所有幫助!
這可能有點麻煩,但是對我有用。 給定來自Blob的內存流,再加上目標的寬度和高度尺寸,它將返回帶有已調整大小的圖像副本的新流。
public static Stream CreateThumbnail(Stream input, Int32 targetWidth, Int32 targetHeight)
{
output = new MemoryStream();
using (Bitmap bitmap = new Bitmap(input))
{
ImageFormat format = bitmap.RawFormat;
Boolean isJpeg = (format.Equals(ImageFormat.Jpeg));
Boolean isPng = (format.Equals(ImageFormat.Png));
Int32 width = bitmap.Width;
Int32 height = bitmap.Height;
getTargetSizes(out width, out height, bitmap, targetWidth, targetHeight);
using (Bitmap thumbnailBitmap = new Bitmap(width, height))
{
Graphics G = Graphics.FromImage(thumbnailBitmap);
G.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
G.DrawImage(bitmap, 0, 0, width, height);
thumbnailBitmap.SetResolution(72, 72);
if (isJpeg)
{
var codecParams = new EncoderParameters(1);
codecParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80L);
ImageCodecInfo[] arrayICI;
ImageCodecInfo jpegICI = null;
arrayICI = ImageCodecInfo.GetImageEncoders();
for (int i = 0; i < arrayICI.Length; i++)
{
if (arrayICI[i].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[i];
break;
}
}
thumbnailBitmap.Save(output, jpegICI, codecParams);
}
else
{
thumbnailBitmap.Save(output, ImageFormat.Png);
}
}
}
return output;
}
private static void getTargetSizes(out Int32 targetWidth, out Int32 targetHeight, Bitmap BM, Int32 maxWidth = 150, Int32 maxHeight = 150)
{
Int32 startWidth = BM.Width;
Int32 startHeight = BM.Height;
targetWidth = startWidth;
targetHeight = startHeight;
Boolean resizeByWidth = false;
Boolean resizeByHeight = false;
if ((maxWidth > 0) && (maxHeight > 0))
{
if ((startWidth > maxWidth) || (startHeight > maxHeight))
{
if (startHeight <= startWidth)
{
if(targetWidth > maxWidth) resizeByWidth = true;
}
else
{
if(targetHeight > maxHeight) resizeByHeight = true;
}
}
}
else if (maxWidth > 0)
{
// Resize within width only
if (startWidth > maxWidth)
{
if (targetWidth > maxWidth) resizeByWidth = true;
}
}
else if (maxHeight > 0)
{
// Resize by height only
if (startHeight > maxHeight)
{
if (targetHeight > maxHeight) resizeByHeight = true;
}
}
if (resizeByWidth)
{
targetWidth = maxWidth;
targetHeight = (Int32)(startHeight * ((Decimal)targetWidth / (Decimal)startWidth));
}
if (resizeByHeight)
{
targetHeight = maxHeight;
targetWidth = (Int32)(startWidth * ((Decimal)targetHeight / (Decimal)startHeight));
}
}
}
並用類似這樣的名稱:
MemoryStream strm = new MemoryStream(returnImage(id, column));
strm = CreateThumbnail(strm, 100, 100);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.