C# 从BLOB转换图像后,如何调整图像大小?

C# 从BLOB转换图像后,如何调整图像大小?,c#,.net,resize,blob,generic-handler,C#,.net,Resize,Blob,Generic Handler,我正在尝试在从数据库中的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 { pub

我正在尝试在从数据库中的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;
        }
    }

}

使用制度;
使用System.Web;
使用系统数据;
使用System.Data.SqlClient;
使用System.IO;
公共类映像:IHttpHandler{
公共void ProcessRequest(HttpContext上下文){
Guid id=新Guid(context.Request.QueryString[“id”]);
int列=7;
if(context.Request.QueryString[“img”]=“tbn”)
{
列=6;
}
context.Response.ContentType=“image/png”;
MemoryStream strm=新的MemoryStream(返回图像(id,列));
字节[]缓冲区=新字节[4096];
int byteSeq=strm.Read(缓冲区,04096);
while(byteSeq>0)
{
context.Response.OutputStream.Write(缓冲区,0,字节相等);
byteSeq=strm.Read(缓冲区,04096);
}
}
公共字节[]返回图像(Guid id,int列)
{
SqlConnection sqlCn=newsqlconnection(“数据源=localhost;初始目录=database;用户ID=User;密码=Password”);
string qry=“从项目中选择*,其中Id=@Id”;
SqlCommand cmd=新的SqlCommand(qry,sqlCn);
cmd.Parameters.Add(“@id”,SqlDbType.UniqueIdentifier).Value=id;
sqlCn.Open();
SqlDataReader dr=cmd.ExecuteReader();
里德博士();
字节[]ar=(字节[])(dr[列]);
Close博士();
cmd.Dispose();
sqlCn.Close();
返回ar;
}
公共布尔可重用{
得到{
返回false;
}
}
}
它应该查看图像宽度是否高于其高度,反之亦然,并根据设置的高度或宽度以及其他值(高度/宽度)应按比例设置,以便不会拉伸

我发现的是:


但我真的不知道如何使用它…有什么建议吗?提前谢谢你的帮助

这可能有点冗长,但对我来说很有效。给定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);