![](/img/trans.png)
[英]How to reduce image file, resize without losing quality using MultipleFileUpload in ASP.NET C#
[英]How to resize an image after upload it in ASP.net using c#?
我嘗試上傳圖像並將其保存在主機上,然后立即調整其大小並將新圖像保存到主機上的其他位置。 這是我的代碼:
--------------- btnStart()-----------------
//1-get web path
string path = Server.MapPath("..") + "\\images\\";
string thumbPath = Server.MapPath("..") + "\\thumbnails\\";
path += folder;
thumbPath += folder;
//2-get and check file extension
String[] validext = { ".jpg", ".png" };
string ext = System.IO.Path.GetExtension(UploadPicture.PostedFile.FileName);
if (Array.IndexOf(validext, ext.ToLower()) < 0)
{
lblMessage.Text ="this file is not allowed";
return;
}
//3-get and check file size
long size = UploadPicture.PostedFile.ContentLength;
size = size / 1024;
if (size > 1300)
{
lblMessage.Text ="incorrect size";
return;
}
//4-get file name
string filename = System.IO.Path.GetFileName(UploadPicture.PostedFile.FileName);
lblFileName.Text = filename;
//5-check file exist and if (true) generate new name
while (System.IO.File.Exists(path + "\\" + filename))
filename = "1" + filename;
//6-save file to server
UploadPicture.PostedFile.SaveAs(path + filename);
changeImageSize cis = new changeImageSize();
Bitmap bm = new Bitmap(path + filename);
-----------結束事件--------------------
----------- changeImageSize類--------
public class changeImageSize
{
public changeImageSize() { }
/// <summary>
/// Method to resize, convert and save the image.
/// </summary>
/// <param name="image">Bitmap image.</param>
/// <param name="maxWidth">resize width.</param>
/// <param name="maxHeight">resize height.</param>
/// <param name="quality">quality setting value.</param>
/// <param name="filePath">file path.</param>
public void Save(Bitmap image, int maxWidth, int maxHeight, int quality, string filePath)
{
// Get the image's original width and height
int originalWidth = image.Width;
int originalHeight = image.Height;
// To preserve the aspect ratio
float ratioX = (float)maxWidth / (float)originalWidth;
float ratioY = (float)maxHeight / (float)originalHeight;
float ratio = Math.Min(ratioX, ratioY);
// New width and height based on aspect ratio
int newWidth = (int)(originalWidth * ratio);
int newHeight = (int)(originalHeight * ratio);
// Convert other formats (including CMYK) to RGB.
Bitmap newImage = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);
// Draws the image in the specified size with quality mode set to HighQuality
using (Graphics graphics = Graphics.FromImage(newImage))
{
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.DrawImage(image, 0, 0, newWidth, newHeight);
}
// Get an ImageCodecInfo object that represents the JPEG codec.
ImageCodecInfo imageCodecInfo = this.GetEncoderInfo(ImageFormat.Jpeg);
// Create an Encoder object for the Quality parameter.
Encoder encoder = Encoder.Quality;
// Create an EncoderParameters object.
EncoderParameters encoderParameters = new EncoderParameters(1);
// Save the image as a JPEG file with quality level.
EncoderParameter encoderParameter = new EncoderParameter(encoder, quality);
encoderParameters.Param[0] = encoderParameter;
newImage.Save(filePath, imageCodecInfo, encoderParameters);
}
/// <summary>
/// Method to get encoder infor for given image format.
/// </summary>
/// <param name="format">Image format</param>
/// <returns>image codec info.</returns>
private ImageCodecInfo GetEncoderInfo(ImageFormat format)
{
return ImageCodecInfo.GetImageDecoders().SingleOrDefault(c => c.FormatID == format.Guid);
}
}
-----------結束課程--------------------
如您在btnStart()事件代碼上看到的那樣,我想要在上載圖像后,以新的大小制作一個副本,並將其保存到另一個文件夾(縮略圖)。
但出現以下錯誤:
您的代碼運行良好。 只需檢查以下內容:
1)您的圖像沒有損壞。
2)嘗試指定不同的質量級別25、50、75。
3)在這種情況下,無法使用您指定的maxWidth,maxHeight。
4)檢查您的文件路徑。
編輯:
我將1920x1080圖像的質量級別設置為75,並嘗試將maxWidth和maxHeight設置為800x600、320x240。 它運作良好。
ChangeImageSize cis = new ChangeImageSize();
Bitmap b = new Bitmap(@"C:\Users\Alex Len Paul\Desktop\1\1920x1080.jpg");
cis.Save(b, 320, 240, 75, @"C:\Users\Alex Len Paul\Desktop\1\1920x1080_2.jpg");
我認為這是因為在主機上保存了文件夾,所以您必須授予寫權限才能在主機上保存路徑。 您的代碼無法在主機中保存圖像
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.