ASP.NET Image uploading with Resizing

前端 未结 14 1832
广开言路
广开言路 2020-11-30 01:27

I have an aspx page which will upload images to server harddisk from client pc

But now i need to change my program in such a way that it would allow me to resize the

相关标签:
14条回答
  • 2020-11-30 01:55
    public string ResizeImageAndSave(int Width, int Height, string imageUrl, string destPath)
        {
            System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(imageUrl);
     double widthRatio = (double)fullSizeImg.Width / (double)Width;
      double heightRatio = (double)fullSizeImg.Height / (double)Height;
      double ratio = Math.Max(widthRatio, heightRatio);
     int newWidth = (int)(fullSizeImg.Width / ratio);
     int newHeight = (int)(fullSizeImg.Height / ratio);
            //System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
            System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(newWidth, newHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
     //DateTime MyDate = DateTime.Now;
     //String MyString = MyDate.ToString("ddMMyyhhmmss") + imageUrl.Substring(imageUrl.LastIndexOf("."));
     thumbNailImg.Save(destPath, ImageFormat.Jpeg);
     thumbNailImg.Dispose();
            return "";
        }
        public bool ThumbnailCallback() { return false; }
    
    0 讨论(0)
  • 2020-11-30 01:58

    You'll need to use the WebClient class to download the remote image.

    After that, then you can resize it...Use DrawImage, not GetThumbnailImage. Make sure you dispose of your bitmap and graphics handles.. (use using{}). Set all quality settings to high.

    You might want to take a look at the source code for my popular image resizer first... It will help you avoid some common trouble areas.

    0 讨论(0)
提交回复
热议问题