本文借助vs中自带的FileUpload控件实现图片文件的上传并生成缩略图 实现过程选择图片上传成功后取得已经存在服务器的文件生成缩略图并且判断是否是图片类型的文件这个的判断可以在程序中修改本程序只是判断了image/bmpimage/gifimage/pjpeg三种类型 代码如下 upfileaspx文件 <%@ Page Language=C# AutoEventWireup=true CodeFile=upfileaspxcs Inherits=upfile_upfile %> <!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd> <html xmlns= > <head runat=server> <title>无标题页</title> </head> <body> <form id=form runat=server> <div> <asp:FileUpload ID=FileUpload runat=server /> <asp:Button ID=Button runat=server OnClick=Button_Click Text=上传 /><br /> <asp:Label ID=Label runat=server></asp:Label></div> </form> </body> </html> upfileaspxcs文件 using System; using SystemData; using SystemConfiguration; using SystemCollections; using SystemWeb; using SystemWebSecurity; using SystemWebUI; using SystemWebUIWebControls; using SystemWebUIWebControlsWebParts; using SystemWebUIHtmlControls; using SystemIO; public partial class upfile_upfile : SystemWebUIPage { protected void Page_Load(object sender EventArgs e) { } protected void Button_Click(object sender EventArgs e) { if (FileUploadHasFile) { string fileContentType = FileUploadPostedFileContentType; if (fileContentType == image/bmp || fileContentType == image/gif || fileContentType == image/pjpeg) { string name = FileUploadPostedFileFileName; // 客户端文件路径 FileInfo file = new FileInfo(name); string fileName = fileName; // 文件名称 string fileName_s = s_ + fileName; // 缩略图文件名称 string fileName_sy = sy_ + fileName; // 水印图文件名称(文字) string fileName_syp = syp_ + fileName; // 水印图文件名称(图片) string webFilePath = ServerMapPath(file/ + fileName); // 服务器端文件路径 string webFilePath_s = ServerMapPath(file/ + fileName_s);// 服务器端缩略图路径 string webFilePath_sy = ServerMapPath(file/ + fileName_sy);// 服务器端带水印图路径(文字) string webFilePath_syp = ServerMapPath(file/ + fileName_syp);// 服务器端带水印图路径(图片) string webFilePath_sypf = ServerMapPath(file/shuiyinjpg);// 服务器端水印图路径(图片) if (!FileExists(webFilePath)) { try { FileUploadSaveAs(webFilePath); // 使用 SaveAs 方法保存文件 AddShuiYinWord(webFilePath webFilePath_sy); AddShuiYinPic(webFilePath webFilePath_syp webFilePath_sypf); MakeThumbnail(webFilePath webFilePath_s Cut); // 生成缩略图方法 LabelText = 提示文件 + fileName + 成功上传并生成 + fileName_s + 缩略图文件类型为 + FileUploadPostedFileContentType + 文件大小为 + FileUploadPostedFileContentLength + B; } catch (Exception ex) { LabelText = 提示文件上传失败失败原因 + exMessage; } } else { LabelText = 提示文件已经存在请重命名后上传; } } else { LabelText = 提示文件类型不符; } } } /**//// <summary> /// 生成缩略图 /// </summary> /// <param name=originalImagePath>源图路径(物理路径)</param> /// <param name=thumbnailPath>缩略图路径(物理路径)</param> /// <param name=width>缩略图宽度</param> /// <param name=height>缩略图高度</param> /// <param name=mode>生成缩略图的方式</param> public static void MakeThumbnail(string originalImagePath string thumbnailPath int width int height string mode) { SystemDrawingImage originalImage = SystemDrawingImageFromFile(originalImagePath); int towidth = width; int toheight = height; int x = ; int y = ; int ow = originalImageWidth; int oh = originalImageHeight; switch (mode) { case HW://指定高宽缩放(可能变形) break; case W://指定宽高按比例 toheight = originalImageHeight * width / originalImageWidth; break; case H://指定高宽按比例 towidth = originalImageWidth * height / originalImageHeight; break; case Cut://指定高宽裁减(不变形) if ((double)originalImageWidth / (double)originalImageHeight > (double)towidth / (double)toheight) { oh = originalImageHeight; ow = originalImageHeight * towidth / toheight; y = ; x = (originalImageWidth ow) / ; } else { ow = originalImageWidth; oh = originalImageWidth * height / towidth; x = ; y = (originalImageHeight oh) / ; } break; default: break; } //新建一个bmp图片 SystemDrawingImage bitmap = new SystemDrawingBitmap(towidth toheight); //新建一个画板 SystemDrawingGraphics g = SystemDrawingGraphicsFromImage(bitmap); //设置高质量插值法 gInterpolationMode = SystemDrawingDrawingDInterpolationModeHigh; //设置高质量低速度呈现平滑程度 gSmoothingMode = SystemDrawingDrawingDSmoothingModeHighQuality; //清空画布并以透明背景色填充 gClear(SystemDrawingColorTransparent); //在指定位置并且按指定大小绘制原图片的指定部分 gDrawImage(originalImage new SystemDrawingRectangle( towidth toheight) new SystemDrawingRectangle(x y ow oh) SystemDrawingGraphicsUnitPixel); try { //以jpg格式保存缩略图 bitmapSave(thumbnailPath SystemDrawingImagingImageFormatJpeg); } catch (SystemException e) { throw e; } finally { originalImageDispose(); bitmapDispose(); gDispose(); } } /**//// <summary> /// 在图片上增加文字水印 /// </summary> /// <param name=Path>原服务器图片路径</param> /// <param name=Path_sy>生成的带文字水印的图片路径</param> protected void AddShuiYinWord(string Path string Path_sy) { string addText = 测试水印; SystemDrawingImage image = SystemDrawingImageFromFile(Path); SystemDrawingGraphics g = SystemDrawingGraphicsFromImage(image); gDrawImage(image imageWidth imageHeight); SystemDrawingFont f = new SystemDrawingFont(Verdana ); SystemDrawingBrush b = new SystemDrawingSolidBrush(SystemDrawingColorBlue); gDrawString(addText f b ); gDispose(); imageSave(Path_sy); imageDispose(); } /**//// <summary> /// 在图片上生成图片水印 /// </summary> /// <param name=Path>原服务器图片路径</param> /// <param name=Path_syp>生成的带图片水印的图片路径</param> /// <param name=Path_sypf>水印图片路径</param> protected void AddShuiYinPic(string Path string Path_syp string Path_sypf) { SystemDrawingImage image = SystemDrawingImageFromFile(Path); SystemDrawingImage copyImage = SystemDrawingImageFromFile(Path_sypf); SystemDrawingGraphics g = SystemDrawingGraphicsFromImage(image); gDrawImage(copyImage new SystemDrawingRectangle(imageWidth copyImageWidth imageHeight copyImageHeight copyImageWidth copyImageHeight) copyImageWidthcopyImageHeight SystemDrawingGraphicsUnitPixel); gDispose(); imageSave(Path_syp); imageDispose(); } } |