jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
ÿþ//======================================================================
//        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<h_lbc
      /// <summary>
      /// þVGr<h_lbc
      /// </summary>
      /// <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/fGIF<h_êS/ec256r‚þVÏP0
      public static bool ConvertImage(string OriFilename, string DesiredFilename)
      {
          string extname = DesiredFilename.Substring(DesiredFilename.LastIndexOf('.')+1).ToLower();
          ImageFormat DesiredFormat;
          //9hncib _ T ÿcš[ImageFormat
          switch (extname)
          {
              case "bmp":
                  DesiredFormat = ImageFormat.Bmp;
                  break;
              case "gif":
                  DesiredFormat = ImageFormat.Gif;
                  break;
              case "jpeg":
                  DesiredFormat = ImageFormat.Jpeg;
                  break;
              case "ico":
                  DesiredFormat = ImageFormat.Icon;
                  break;
              case "png":
                  DesiredFormat = ImageFormat.Png;
                  break;
              default:
                  DesiredFormat = ImageFormat.Jpeg;
                  break;
          }
          try
          {
              System.Drawing.Image imgFile = System.Drawing.Image.FromFile(WebPathTran(OriFilename));
              imgFile.Save(WebPathTran(DesiredFilename), DesiredFormat);
              return true;
          }
          catch 
          {
              return false;
          }
      }
        #endregion
 
 
      #region þVGr)>e
      /// <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<h_</param>
      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;
      }
    }
}