我有一個頁面,允許用戶上傳圖片。問題是我的舊用戶無法自己調整圖像大小。我想允許他們上傳任意大小的圖像,然後當服務器獲取它時,它會創建這張圖片的一個小副本。在Aspx中上傳和調整圖像大小
4
A
回答
14
有來調整圖像,所以很多的方法,但是我喜歡這個
System.Drawing.Image image = System.Drawing.Image.FromFile("FilePath");
int newwidthimg = 160;
float AspectRatio = (float)image.Size.Width/(float)image.Size.Height;
int newHeight = Convert.ToInt32(newwidthimg/AspectRatio);
Bitmap thumbnailBitmap = new Bitmap(newwidthimg, newHeight);
Graphics thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, newwidthimg, newHeight);
thumbnailGraph.DrawImage(image, imageRectangle);
thumbnailBitmap.Save("FilePath", ImageFormat.Jpeg);
thumbnailGraph.Dispose();
thumbnailBitmap.Dispose();
image.Dispose();
我有固定的寬度,因爲我想有寬度160我的所有圖片和高度根據長寬比
8
您可以使用某種調整大小方法。
protected void Button1_Click(object sender, EventArgs e)
{
HttpPostedFile pf = FileUpload1.PostedFile;
System.Drawing.Image bm = System.Drawing.Image.FromStream(pf.InputStream);
bm = ResizeBitmap((Bitmap) bm, 100, 100); /// new width, height
bm.Save(Path.Combine(YOURUPLOADPATH, pf.FileName);
}
private Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
{
Bitmap result = new Bitmap(nWidth, nHeight);
using (Graphics g = Graphics.FromImage((System.Drawing.Image)result))
g.DrawImage(b, 0, 0, nWidth, nHeight);
return result;
}
在另一方面,上傳一個如3 MB的文件和事後 * 調整 *到一個20KB PIC - 不不聲音很好給我。也許你可以考慮限制上傳文件的大小。
1
這是一個相當新的解決方案。希望它可以幫助
http://forums.asp.net/t/1657138.aspx/1?C+Resize+Profile+Image
protected void UploadButton_Click(object sender, EventArgs e)
{
if (File1.HasFile)
{
string fileName = Server.HtmlEncode(File1.FileName);
string extension = System.IO.Path.GetExtension(fileName);
System.Drawing.Image image_file = System.Drawing.Image.FromStream(File1.PostedFile.InputStream);
int image_height = image_file.Height;
int image_width = image_file.Width;
int max_height = 100;
int max_width = 100;
image_height = (image_height * max_width)/image_width;
image_width = max_width;
if (image_height > max_height)
{
image_width = (image_width * max_height)/image_height;
image_height = max_height;
}
Bitmap bitmap_file = new Bitmap(image_file, image_width, image_height);
System.IO.MemoryStream stream = new System.IO.MemoryStream();
bitmap_file.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Position = 0;
byte[] data = new byte[stream.Length + 1];
stream.Read(data, 0, data.Length);
Profile.Picture = data;
Profile.PictureType = "image/png";
}
}
相關問題
- 1. 調整上傳圖像的大小 - PHP
- 2. symfony圖像上傳調整大小
- 3. 調整圖像大小並上傳
- 4. Symfony2:調整上傳圖像的大小
- 5. 上傳的WordPress圖像大小調整
- 6. 上傳後調整圖像大小!
- 7. 在.aspx網頁上顯示調整大小的圖像
- 8. 調整大小和上傳圖像流中的NodeJS
- 9. 圖像上傳小部件,調整圖像大小然後上傳
- 10. 在畫布上調整大小和重新定位圖像調整大小
- 11. 在Drupal中使用imagecopyresampled調整上傳圖像的大小
- 12. 在MVC 6中調整上傳圖像的大小6
- 13. Django在上傳過程中調整圖像大小
- 14. 在onDoubleTapEvent上調整圖像大小
- 15. 調整圖像大小在畫布上
- 16. CGAffineTransformMakeRotation()在iPad上調整圖像大小?
- 17. 圖像上傳時調整圖像的大小asp.net
- 18. php圖片調整大小不能在圖像上傳
- 19. Png黑色背景上傳和調整圖像大小
- 20. codeigniter圖像上傳和調整大小 - swfupload
- 21. HTML畫布 - 拖動和調整上傳圖像的大小
- 22. 調整圖像大小和縮略圖
- 23. kohana 3.0在上傳前調整圖像大小?
- 24. php圖像調整大小並在上傳時轉換爲jpg
- 25. 在vb上傳之前調整圖像大小
- 26. CURL在上傳到Imgur之前調整圖像大小
- 27. 在上傳之前調整圖像大小
- 28. 在文件上傳時調整圖像大小 - C#
- 29. 更改代碼以在上傳時調整圖像大小
- 30. 如何在上傳到coudinary之前調整圖像大小?
我喜歡這個選項直接從HttpPost中獲取圖像 - 並且不依賴於先將圖像保存到磁盤 – 2017-08-02 15:58:34