我对图像尺寸有疑问。当我上传 500*500 尺寸或其他尺寸的图像时,在这种情况下,我想要 1024*768 尺寸的图像输出而不压缩。意味着,在图像背景上自动添加其他空间(作为黑色背景或其他)。我该怎么做,任何建议。
第一次我尝试在固定大小的图像控件上显示图像。
.aspx
<asp:Image ID="ImagePreview1" BorderColor="Aqua"
BorderWidth="1px" runat="server" Height="768px" Width="1024px" />
然后单击按钮保存特定文件夹的图像..
.aspx.cs
protected void btnsave_Click(object sender, EventArgs e)
{
string filepath = ImagePreview1.ImageUrl;
using (WebClient client = new WebClient())
{
client.DownloadFile(filepath, Server.MapPath("~/imgprev/pic.jpg"));
}
}
在这两者之间,我也使用此代码。此代码用于压缩图像大小..
System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(imagePath);
var thumbnailImg = new Bitmap(1024, 768);
var thumbGraph = Graphics.FromImage(thumbnailImg);
thumbGraph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
thumbGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
thumbGraph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, 1024, 768);
thumbGraph.DrawImage(fullSizeImg, imageRectangle);
string targetPath = imagePath.Replace(Path.GetFileNameWithoutExtension(imagePath), Path.GetFileNameWithoutExtension(imagePath) + "-resize");
string SName = Path.GetFileName(targetPath);
string SImgName = ("~/imgprev/") + SName;
//string st = targetPath.ToString();
//string stArr = st.Split('\\')[8];
thumbnailImg.Save(targetPath, System.Drawing.Imaging.ImageFormat.Jpeg); //(A generic error occurred in GDI+) Error occur here !
Label1.Text = SImgName.ToString();
thumbnailImg.Dispose();
return targetPath;