简体   繁体   English

使用 ASP.NET 从 SQL Server 检索时图像质量下降

[英]Image quality decrease when retrieving from SQL Server using ASP.NET

I'm using ASP.NET to build a webpage which allow user to upload images.我正在使用 ASP.NET 构建一个允许用户上传图像的网页。 I use the following code to convert image to image byte:我使用以下代码将图像转换为图像字节:

FileUpload img = (FileUpload)fileUploadLocationPic;
Byte[] imgByte = null;
string strFileExtension = Path.GetExtension(img.PostedFile.FileName.ToString());

// Create a FILE
HttpPostedFile File = fileUploadLocationPic.PostedFile;

// Create byte array
imgByte = new Byte[File.ContentLength];

// force control to load data
File.InputStream.Read(imgByte, 0, File.ContentLength);

Then I save imgByte into SQL Server.然后我将imgByte保存到 SQL Server 中。 But when I retrieve image byte from SQL Server, I can see that the image quality has decreased.但是当我从 SQL Server 检索图像字节时,我可以看到图像质量有所下降。 Can anyone tell me how to solve this problem?谁能告诉我如何解决这个问题? Thanks in advance.提前致谢。

EDIT: The way I show image is using ashx handler:编辑:我显示图像的方式是使用ashx处理程序:

        SqlConnection connection = new SqlConnection(connectionString);
        string sql = "SELECT [ChargingBoxLocationImage] FROM [Parking Lot] WHERE [ID] = @PLID";
        SqlCommand cmd = new SqlCommand(sql, connection);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@PLID", PLID);
        connection.Open();
        object img = cmd.ExecuteScalar();
        try
        {
            return new MemoryStream((byte[])img);
        }

And then in aspx , use然后在aspx ,使用

imageChargingLocation.ImageUrl = "~/Editor/ShowImage.ashx?PLID=" + strPLID;

原来的取回

It will be obvious to see the difference if you zoom in.如果放大,会很明显地看到差异。

The code you have posted is fine since you are copying the bytes directly from the image.您发布的代码很好,因为您直接从图像复制字节。 What does your code look like that reads the data and sends it to the client?读取数据并将其发送给客户端的代码是什么样的?

As long as you don't load the bytes into a Bitmap and then call Bitmap.Save , your code will not decrease the image quality.只要您不将字节加载Bitmap.Save Bitmap然后调用Bitmap.Save ,您的代码就不会降低图像质量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM