ÿþ//====================================================================== // Copyright (C) 2008-2009 // All rights reserved // // Filename :ImageOp // Description : // //  Created by Conan at  2009-3-6 16:39:35 //  http://conan87810.cnblogs.com //====================================================================== using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Drawing.Imaging; using System.Drawing; using System.Web; namespace HH.WMS.Utils { public static class ZImage { #region þVGr /// þVGr /// <param name="OriFilename">ŸSËY‡eöNøvù[_</param> /// <param name="DesiredFilename">ubîvh‡eöNøvù[_</param> /// <returns></returns> /// JPGǑ(u„v/f g_c‹S)@båNJPGþVÏP gïSý€OM–NOþVÏPnpf¦^ ÿ €ÏP }/f NOM–NO„v /// GIFǑ(u„v/fàe_c‹S)@båNGIFþVÏP/f NOM–NOŸSþVþVÏPnpf¦^ŒTÏP }„v ÿFO/fGIFe /// <summary> /// þVGrúVš['Y\)>e /// </summary> /// <param name="OriFileName">n‡eöNøvù[0W@W</param> /// <param name="DesiredFilename">îvh‡eöNøvù[0W@W</param> /// <param name="IntWidth">îvh‡eöN½[</param> /// <param name="IntHeight">îvh‡eöNؚ</param> /// <param name="imageFormat">þVGr‡eöN public static bool ChangeImageSize(string OriFileName, string DesiredFilename, int IntWidth, int IntHeight, ImageFormat imageFormat) { string SourceFileNameStr =WebPathTran(OriFileName); //egnþVGr Tðy_ string TransferFileNameStr = WebPathTran(DesiredFilename); //îv„vþVGr Tðy_ FileStream myOutput =null; try { System.Drawing.Image.GetThumbnailImageAbort myAbort = new System.Drawing.Image.GetThumbnailImageAbort(imageAbort); Image SourceImage = System.Drawing.Image.FromFile(OriFileName);//egnþVGrš[IN Image TargetImage = SourceImage.GetThumbnailImage(IntWidth, IntHeight, myAbort, IntPtr.Zero); //îv„vþVGrš[IN //\TargetFileNameStr„vþVGr>e½[:NIntWidth ÿؚ:NIntHeight myOutput = new FileStream(TransferFileNameStr, FileMode.Create, FileAccess.Write, FileShare.Write); TargetImage.Save(myOutput, imageFormat); myOutput.Close(); return true; } catch { myOutput.Close(); return false; } } private static bool imageAbort() { return false; } #endregion #region ‡eW[4lpS /// <summary> /// ‡eW[4lpS /// </summary> /// <param name="wtext">4lpS‡eW[</param> /// <param name="source">ŸSþVGrirt‡eöN T</param> /// <param name="target">ubþVGrirt‡eöN T</param> public static bool ImageWaterText(string wtext,string source, string target) { bool resFlag = false; Image image = Image.FromFile(source); Graphics graphics = Graphics.FromImage(image); try { graphics.DrawImage(image, 0, 0, image.Width, image.Height); Font font = new System.Drawing.Font("Verdana", 60); Brush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Green); graphics.DrawString(wtext, font, brush, 35, 35); image.Save(target); resFlag = true; } catch (Exception) { throw; } finally { graphics.Dispose(); image.Dispose(); } return resFlag; } #endregion #region þVGr4lpS /// <summary> /// (WþVGr NubþVGr4lpS /// </summary> /// <param name="Path">ŸS g¡RhVþVGr_</param> /// <param name="Path_syp">ub„v&^þVGr4lpS„vþVGr_</param> /// <param name="Path_sypf">4lpSþVGr_</param> public static bool ImageWaterPic(string source, string target, string waterPicSource) { bool resFlag = false; Image sourceimage = Image.FromFile(source); Graphics sourcegraphics = Graphics.FromImage(sourceimage); Image waterPicSourceImage = Image.FromFile(waterPicSource); try { sourcegraphics.DrawImage(waterPicSourceImage, new System.Drawing.Rectangle((sourceimage.Width - waterPicSourceImage.Width)/2, (sourceimage.Height - waterPicSourceImage.Height)/2, waterPicSourceImage.Width, waterPicSourceImage.Height), 0, 0, waterPicSourceImage.Width, waterPicSourceImage.Height, GraphicsUnit.Pixel); sourceimage.Save(target); resFlag = true; } catch (Exception) { throw; } finally { sourcegraphics.Dispose(); sourceimage.Dispose(); waterPicSourceImage.Dispose(); } return resFlag; } #endregion /// <summary> /// _lbcÿlbcbÝ~ù[_ ÿ /// </summary> /// <param name="path"></param> /// <returns></returns> private static string WebPathTran(string path) { try { return HttpContext.Current.Server.MapPath(path); } catch { return path; } } /// \ž[E–MOn-N„vgqGrlS:Nbyte[]{|‹W™QeQpenc“^-N /// </summary> /// <param name="strFile">stringþVGr0W@W</param> /// <returns>byte[]</returns> public static byte[] GetBytesByImagePath(string strFile) { byte[] photo_byte = null; using (FileStream fs = new FileStream(strFile, FileMode.Open, FileAccess.Read)) { using (BinaryReader br = new BinaryReader(fs)) { photo_byte = br.ReadBytes((int)fs.Length); } } return photo_byte; } } }