发一些自己最常用的C#函数(二) :上传图片处理
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了发一些自己最常用的C#函数(二) :上传图片处理,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7062字,纯文字阅读大概需要11分钟。
内容图文
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; namespace ZooleeBasic { public class ZImage { private static readonly string[] ImageFileExtensions = { ".bmp", ".gif", ".jpg", ".jpeg", ".png" }; privatestaticreadonly ImageFormat[] ImageFormats = { ImageFormat.Bmp, ImageFormat.Gif, ImageFormat.Jpeg, ImageFormat.Jpeg, ImageFormat.Png }; ///<summary>/// 缩放模式 ///</summary>publicenum ResizeMod { 定宽定高简单缩放 = 1, 定宽等比例缩放 = 2, 定高等比例缩放 = 3, 定宽定高等比例缩放 = 4 } ///<summary>/// 水印位置 ///</summary>publicenum WatermarkPosition { 左上 = 1, 左中 = 2, 左下 = 3, 中上 = 4, 正中 = 5, 中下 = 6, 右上 = 7, 右中 = 8, 右下 = 9 } ///<summary>/// 是否图像文件 ///</summary>///<param name="filePath">文件的物理路径</param>///<returns></returns>publicstaticbool IsImageFile(string filePath) { string ext = ZFile.GetFileExtension(filePath); if(ZString.IsInArray(ext,ImageFileExtensions,false)){returntrue;} returnfalse; } ///<summary>/// 获得一个文件的图像文件格式(非图像文件返回null) ///</summary>///<param name="filePath">文件的物理路径</param>///<returns>返回图像文件格式</returns>publicstatic ImageFormat GetImageFormat(string filePath) { string ext = ZFile.GetFileExtension(filePath); int id = ZString.GetIDInArray(ext, ImageFileExtensions, false); if (id < 0) { returnnull; } return ImageFormats[id]; } ///<summary>/// 缩放图像文件 ///</summary>///<param name="oFilePath">源文件的物理路径</param>///<param name="toFilePath">新文件的物理路径</param>///<param name="toWidth">新的图像宽度</param>///<param name="toHeight">新的图像高度</param>///<param name="resizeMod">缩放模式</param>publicstaticvoid Resize(string oFilePath, string toFilePath, int toWidth, int toHeight, ResizeMod resizeMod) { if(!IsImageFile(oFilePath)){ return; } Image oImage = Image.FromFile(oFilePath); int ow = oImage.Width; int oh = oImage.Height; int tow = toWidth; int toh = toHeight; int x = 0; int y = 0; switch (resizeMod) { case ResizeMod.定宽定高简单缩放: break; case ResizeMod.定宽等比例缩放: toh = oImage.Height * toWidth / oImage.Width; break; case ResizeMod.定高等比例缩放: tow = oImage.Width * toHeight / oImage.Height; break; case ResizeMod.定宽定高等比例缩放: if ((double)oImage.Width / (double)oImage.Height > (double)tow / (double)toh) { oh = oImage.Height; ow = oImage.Height * tow / toh; y = 0; x = (oImage.Width - ow) / 2; } else { ow = oImage.Width; oh = oImage.Width * toHeight / tow; x = 0; y = (oImage.Height - oh) / 2; } break; default: break; } Image bitmap = new Bitmap(tow, toh);//新建一个bmp图片 Graphics g = Graphics.FromImage(bitmap);//新建一个画板 g.InterpolationMode = InterpolationMode.High;//设置高质量插值法 g.SmoothingMode = SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度 g.Clear(System.Drawing.Color.Transparent);//清空画布并以透明背景色填充 g.DrawImage(oImage, new Rectangle(0, 0, tow, toh), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);//在指定位置并且按指定大小绘制原图片的指定部分try { ZFile.CreateDirectory(ZFile.GetFileDiretory(toFilePath)); bitmap.Save(toFilePath, GetImageFormat(oFilePath));//以原图片格式保存缩放图 } catch (System.Exception e) { throw e; } finally { oImage.Dispose(); bitmap.Dispose(); g.Dispose(); } } ///<summary>/// 加图片水印 ///</summary>///<param name="oFilePath">源文件的物理路径</param>///<param name="toFilePath">新文件的物理路径</param>///<param name="watermarkPath">水印图片的物理路径</param>///<param name="watermarkPosition">水印位置</param>publicstaticvoid ImageWatermark(string oFilePath, string toFilePath, string watermarkPath, WatermarkPosition watermarkPosition) { string ext = ZFile.GetFileExtension(oFilePath); if (!ZString.IsInArray(ext, ImageFileExtensions, false)) { return; } int x = 0; int y = 0; Image img = Bitmap.FromFile(oFilePath); Image waterimg = Image.FromFile(watermarkPath); switch(watermarkPosition) { case WatermarkPosition.左上: x=10; y=10; break; case WatermarkPosition.左中: x = 10; y = img.Height / 2 - waterimg.Height / 2; break; case WatermarkPosition.左下: x = 10; y = img.Height - waterimg.Height - 10; break; case WatermarkPosition.中上: x = img.Width / 2 - waterimg.Width / 2; y = 10; break; case WatermarkPosition.正中: x = img.Width / 2 - waterimg.Width / 2; y = img.Height / 2 - waterimg.Height / 2; break; case WatermarkPosition.中下: x = img.Width / 2 - waterimg.Width / 2; y = img.Height - waterimg.Height -10; break; case WatermarkPosition.右上: x = img.Width - waterimg.Width - 10; y = 10; break; case WatermarkPosition.右中: x = img.Width - waterimg.Width - 10; y = img.Height / 2 - waterimg.Height / 2; break; case WatermarkPosition.右下: x = img.Width - waterimg.Width - 10; y = img.Height - waterimg.Height - 10; break; default: break; } Graphics g = Graphics.FromImage(img); g.DrawImage(waterimg, new Rectangle(x, y, waterimg.Width, waterimg.Height)); try { ZFile.CreateDirectory(ZFile.GetFileDiretory(toFilePath)); img.Save(toFilePath, GetImageFormat(oFilePath)); } catch (Exception) { throw; } finally { waterimg.Dispose(); img.Dispose(); g.Dispose(); } } ///<summary>/// 加文字水印 ///</summary>///<param name="oFilePath">源文件的物理路径</param>///<param name="toFilePath">新文件的物理路径</param>///<param name="letter">水印文字</param>///<param name="font">水印文字的文本格式</param>///<param name="brush">水印文字的画刷</param>///<param name="watermarkPosition">水印位置</param>publicstaticvoid LetterWatermark(string oFilePath, string toFilePath, string letter, Font font, Brush brush, WatermarkPosition watermarkPosition) { string ext = ZFile.GetFileExtension(oFilePath); if (!ZString.IsInArray(ext, ImageFileExtensions, false)) { return; } float x = 0; float y = 0; Image img = Bitmap.FromFile(oFilePath); float len = ZString.GetStringLength(letter); float size = font.GetHeight(); float letterLength = len * size; float letterHeight = size; switch (watermarkPosition) { case WatermarkPosition.左上: x = 10; y = 10; break; case WatermarkPosition.左中: x = 10; y = img.Height / 2 - letterHeight / 2; break; case WatermarkPosition.左下: x = 10; y = img.Height - letterHeight - 10; break; case WatermarkPosition.中上: x = img.Width / 2 - letterLength / 2; y = 10; break; case WatermarkPosition.正中: x = img.Width / 2 - letterLength / 2; y = img.Height / 2 - letterHeight / 2; break; case WatermarkPosition.中下: x = img.Width / 2 - letterLength / 2; y = img.Height - letterHeight - 10; break; case WatermarkPosition.右上: x = img.Width - letterLength - 10; y = 10; break; case WatermarkPosition.右中: x = img.Width - letterLength - 10; y = img.Height / 2 - letterHeight / 2; break; case WatermarkPosition.右下: x = img.Width - letterLength - 10; y = img.Height - letterHeight - 10; break; default: break; } Graphics g = Graphics.FromImage(img); g.DrawString(letter, font, brush, x, y); try { ZFile.CreateDirectory(ZFile.GetFileDiretory(toFilePath)); img.Save(toFilePath, GetImageFormat(oFilePath)); } catch (Exception) { throw; } finally { img.Dispose(); g.Dispose(); brush.Dispose(); font.Dispose(); } } } }
原文:http://www.cnblogs.com/apollosun123/p/3639591.html
内容总结
以上是互联网集市为您收集整理的发一些自己最常用的C#函数(二) :上传图片处理全部内容,希望文章能够帮你解决发一些自己最常用的C#函数(二) :上传图片处理所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。