FineUI 官方论坛
标题:
为FileUpload添加保存为指定大小图片的方法
[打印本页]
作者:
摇摆
时间:
2014-9-23 12:28
标题:
为FileUpload添加保存为指定大小图片的方法
下载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文件
作者:
xxdddail
时间:
2014-9-23 14:55
{:soso_e179:}顶
作者:
zy32002
时间:
2014-9-24 22:26
代码有许多问题,建议下载个上传图片工具类
作者:
zy32002
时间:
2014-9-24 22:43
可以参考这个
http://bbs.csdn.net/topics/340113949
作者:
zy32002
时间:
2014-9-24 22:44
可以参考这个
http://bbs.csdn.net/topics/340113949
欢迎光临 FineUI 官方论坛 (https://fineui.com/BBS/)
Powered by Discuz! X3.4