C# 以特定大小显示图像,无需压缩即可获得特定大小的图像
我对图像尺寸有问题。 当我上传了500*500维或其他尺寸的图像时,在这种情况下,我希望输出1024*768维的图像而不进行压缩。表示在图像背景上自动添加其他空间(作为黑色背景或其他)。 我怎么办,有什么建议吗 首先,我尝试在图像控件上以固定大小显示图像 .aspx 在此期间,我还使用了此代码。此代码用于压缩图像大小C# 以特定大小显示图像,无需压缩即可获得特定大小的图像,c#,asp.net,html,C#,Asp.net,Html,我对图像尺寸有问题。 当我上传了500*500维或其他尺寸的图像时,在这种情况下,我希望输出1024*768维的图像而不进行压缩。表示在图像背景上自动添加其他空间(作为黑色背景或其他)。 我怎么办,有什么建议吗 首先,我尝试在图像控件上以固定大小显示图像 .aspx 在此期间,我还使用了此代码。此代码用于压缩图像大小 System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(imagePath); v
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;
你能写出你到目前为止试过的东西吗?我已经添加了一些代码。
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;