我的图像尺寸有问题。 当我上传500 * 500尺寸或其他尺寸的图像时,在这种情况下我想要1024 * 768尺寸图像输出没有压缩。意味着,在图像背景上自动添加其他空间(作为黑色背景或其他)。 怎么办,我有什么建议。
1我尝试在固定尺寸的图像控制上显示图像。
的.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;