下载Fineui源码 打开项目找到WebControls\Field.TextField.RealTextField.FileUpload\FileUpload.cs
添加下面代码- /// <summary>
- /// 将上载的图片保存到 Web 服务器上的指定路径
- /// </summary>
- /// <param name="filename">保存的文件的名称</param>
- /// <param name="resize_width">保存图片的宽度</param>
- /// <param name="resize_height">保存图片的高度</param>
- public void SaveImage(string filename, int resize_width, int resize_height)
- {
- SaveImage(filename, resize_width, resize_height, Color.Transparent, System.Drawing.Imaging.ImageFormat.Jpeg);
- }
- /// <summary>
- /// 将上载的图片保存到 Web 服务器上的指定路径
- /// </summary>
- /// <param name="filename">保存的文件的名称</param>
- /// <param name="resize_width">保存图片的宽度</param>
- /// <param name="resize_height">保存图片的高度</param>
- /// <param name="backgroud_color">保存图片不足部分的填充色</param>
- /// <param name="image_format">保存图片的指定格式</param>
- public void SaveImage(string filename, int resize_width, int resize_height, Color backgroud_color, System.Drawing.Imaging.ImageFormat image_format)
- {
- if (HasFile)
- {
- System.Drawing.Image original_image = System.Drawing.Image.FromStream(PostedFile.InputStream);
- int width = original_image.Width;
- int height = original_image.Height;
- int target_width = resize_width;
- int target_height = resize_height;
- int new_width, new_height;
- float target_ratio = (float)target_width / (float)target_height;
- float image_ratio = (float)width / (float)height;
- if (target_ratio > image_ratio)
- {
- new_height = target_height;
- new_width = (int)Math.Floor(image_ratio * (float)target_height);
- }
- else
- {
- new_height = (int)Math.Floor((float)target_width / image_ratio);
- new_width = target_width;
- }
- new_width = new_width > target_width ? target_width : new_width;
- new_height = new_height > target_height ? target_height : new_height;
- System.Drawing.Bitmap final_image = new System.Drawing.Bitmap(target_width, target_height);
- System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(final_image);
- graphic.FillRectangle(new System.Drawing.SolidBrush(backgroud_color), new System.Drawing.Rectangle(0, 0, target_width, target_height));
- int paste_x = (target_width - new_width) / 2;
- int paste_y = (target_height - new_height) / 2;
- graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
- graphic.DrawImage(original_image, paste_x, paste_y, new_width, new_height);
- final_image.Save(filename, image_format);
- }
- }
-
复制代码
编译FineUI源码项目 替换项目的DLL文件
|