【发布时间】:2014-04-08 19:51:11
【问题描述】:
我正在使用 ASP.NET 构建一个允许用户上传图像的网页。我使用以下代码将图像转换为图像字节:
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);
然后我将imgByte 保存到 SQL Server 中。但是当我从 SQL Server 检索图像字节时,我可以看到图像质量下降了。谁能告诉我如何解决这个问题?提前致谢。
编辑:
我显示图像的方式是使用ashx handler:
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);
}
然后在aspx,使用
imageChargingLocation.ImageUrl = "~/Editor/ShowImage.ashx?PLID=" + strPLID;
放大后会很明显地看到差异。
【问题讨论】:
-
如果是完全相同的字节序列,则质量不会降低。我建议您在没有意识到的情况下寻找转换图像的位置。可能在任何地方,从您提供的信息中无法得知。
-
@zespri 我更新了获取图片的方法,请看一下,谢谢!
-
它们的文件大小不同。尺寸也不同。 (一个是三个像素宽)你在数据库中有多大(对于这张图片)?
-
@zespri 这是图片的屏幕截图,只是想让你们看到不同的地方。但是,是的,我不应该使用屏幕截图。我马上修改。
-
@zespri 当我试图保存检索到的图像时,它实际上不是图像类型而是 ShowImage.ashx 类型。是这个原因吗?
标签: c# asp.net sql-server