コード例 #1
0
 public void SaveBmpWithImageCodecInfo(System.Drawing.Image img, string photoPath)
 {
     System.Drawing.Imaging.ImageCodecInfo    encoderInfo       = PictureWaterMark.GetEncoderInfo("image/jpeg");
     System.Drawing.Imaging.Encoder           quality           = System.Drawing.Imaging.Encoder.Quality;
     System.Drawing.Imaging.EncoderParameters encoderParameters = new System.Drawing.Imaging.EncoderParameters(1);
     System.Drawing.Imaging.EncoderParameter  encoderParameter  = new System.Drawing.Imaging.EncoderParameter(quality, 100L);
     encoderParameters.Param[0] = encoderParameter;
     if (img == null)
     {
         return;
     }
     System.Drawing.Bitmap bitmap = null;
     try
     {
         bitmap = new System.Drawing.Bitmap(img);
         bitmap.Save(photoPath, encoderInfo, encoderParameters);
     }
     catch (Exception ex)
     {
         Log.WriteLog("SaveBmpWithImageCodecInfo出错", ex);
     }
     finally
     {
         bitmap.Dispose();
     }
 }
コード例 #2
0
 public System.Drawing.Image Mark(System.Drawing.Image img, MarkType markType, string text, System.Drawing.Image waterImg, int markx, int marky, bool bold, System.Drawing.Color textColor, float transparence, System.Drawing.FontFamily fontFamily, int fontSize, CompressSize compressSize)
 {
     Guid[] frameDimensionsList = img.FrameDimensionsList;
     System.Drawing.Image result;
     for (int i = 0; i < frameDimensionsList.Length; i++)
     {
         Guid guid = frameDimensionsList[i];
         System.Drawing.Imaging.FrameDimension dimension = new System.Drawing.Imaging.FrameDimension(guid);
         if (img.GetFrameCount(dimension) > 1)
         {
             result = img;
             return(result);
         }
     }
     try
     {
         System.Drawing.Imaging.ImageCodecInfo    encoderInfo       = PictureWaterMark.GetEncoderInfo("image/jpeg");
         System.Drawing.Imaging.Encoder           quality           = System.Drawing.Imaging.Encoder.Quality;
         System.Drawing.Imaging.EncoderParameters encoderParameters = new System.Drawing.Imaging.EncoderParameters(1);
         System.Drawing.Imaging.EncoderParameter  encoderParameter  = new System.Drawing.Imaging.EncoderParameter(quality, 100L);
         encoderParameters.Param[0] = encoderParameter;
         if (markType == MarkType.Text)
         {
             System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
             bitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution);
             System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);
             graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
             graphics.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
             graphics.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
             graphics.DrawImage(img, new System.Drawing.Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, System.Drawing.GraphicsUnit.Pixel);
             System.Drawing.Font  textFont = this.TextFont;
             System.Drawing.SizeF sizeF    = default(System.Drawing.SizeF);
             float num = this.TextFont.Size - 2f;
             if ((double)num < 0.0001)
             {
             }
             sizeF = graphics.MeasureString(this.Text, textFont);
             while ((ushort)sizeF.Width > (ushort)img.Width)
             {
                 num = this.TextFont.Size - 2f;
                 if ((double)num < 0.0001)
                 {
                     break;
                 }
                 this.TextFont = new System.Drawing.Font(this.TextFont.FontFamily, num, this.TextFont.Style);
                 sizeF         = graphics.MeasureString(this.Text, this.TextFont);
                 if ((ushort)sizeF.Width < (ushort)this.SourceImage.Width)
                 {
                     break;
                 }
             }
             int alpha = Convert.ToInt32(255f * transparence);
             System.Drawing.Brush brush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(alpha, textColor));
             graphics.DrawString(text, textFont, brush, (float)markx, (float)marky);
             graphics.Dispose();
             img = this.ConvertBmpToNewImg(bitmap, encoderInfo, encoderParameters);
             bitmap.Dispose();
             result = img;
         }
         else
         {
             if (markType == MarkType.Image)
             {
                 float[][] array     = new float[5][];
                 float[][] arg_236_0 = array;
                 int       arg_236_1 = 0;
                 float[]   array2    = new float[5];
                 array2[0]            = 1f;
                 arg_236_0[arg_236_1] = array2;
                 float[][] arg_24D_0 = array;
                 int       arg_24D_1 = 1;
                 float[]   array3    = new float[5];
                 array3[1]            = 1f;
                 arg_24D_0[arg_24D_1] = array3;
                 float[][] arg_264_0 = array;
                 int       arg_264_1 = 2;
                 float[]   array4    = new float[5];
                 array4[2]            = 1f;
                 arg_264_0[arg_264_1] = array4;
                 float[][] arg_278_0 = array;
                 int       arg_278_1 = 3;
                 float[]   array5    = new float[5];
                 array5[3]            = transparence;
                 arg_278_0[arg_278_1] = array5;
                 array[4]             = new float[]
                 {
                     0f,
                     0f,
                     0f,
                     0f,
                     1f
                 };
                 float[][] newColorMatrix = array;
                 System.Drawing.Imaging.ColorMatrix     newColorMatrix2 = new System.Drawing.Imaging.ColorMatrix(newColorMatrix);
                 System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes();
                 imageAttributes.SetColorMatrix(newColorMatrix2, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Default);
                 System.Drawing.Bitmap bitmap2 = new System.Drawing.Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                 bitmap2.SetResolution(img.HorizontalResolution, img.VerticalResolution);
                 System.Drawing.Graphics graphics2 = System.Drawing.Graphics.FromImage(bitmap2);
                 graphics2.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                 graphics2.SmoothingMode      = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                 graphics2.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                 graphics2.DrawImage(img, new System.Drawing.Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, System.Drawing.GraphicsUnit.Pixel);
                 if (waterImg.Width > img.Width || waterImg.Height > img.Height)
                 {
                     System.Drawing.Image.GetThumbnailImageAbort callback = null;
                     System.Drawing.Image image = null;
                     if (this.CompressSize == CompressSize.None)
                     {
                         image = this.WaterImage.GetThumbnailImage(this.SourceImage.Width, this.SourceImage.Height, callback, new IntPtr(0));
                     }
                     else
                     {
                         if (this.CompressSize == CompressSize.Compress4)
                         {
                             image = this.WaterImage.GetThumbnailImage(this.SourceImage.Width / 2, this.SourceImage.Height / 2, callback, new IntPtr(0));
                         }
                         else
                         {
                             if (this.CompressSize == CompressSize.Compress9)
                             {
                                 image = this.WaterImage.GetThumbnailImage(this.SourceImage.Width / 3, this.SourceImage.Height / 3, callback, new IntPtr(0));
                             }
                             else
                             {
                                 if (this.CompressSize == CompressSize.Compress16)
                                 {
                                     image = this.WaterImage.GetThumbnailImage(this.SourceImage.Width / 4, this.SourceImage.Height / 4, callback, new IntPtr(0));
                                 }
                             }
                         }
                     }
                     if (this.CompressSize == CompressSize.Compress25)
                     {
                         image = this.WaterImage.GetThumbnailImage(this.SourceImage.Width / 5, this.SourceImage.Height / 5, callback, new IntPtr(0));
                     }
                     if (this.CompressSize == CompressSize.Compress36)
                     {
                         image = this.WaterImage.GetThumbnailImage(this.SourceImage.Width / 6, this.SourceImage.Height / 6, callback, new IntPtr(0));
                     }
                     graphics2.DrawImage(image, new System.Drawing.Rectangle(markx, marky, image.Width, image.Height), 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel, imageAttributes);
                     image.Dispose();
                     graphics2.Dispose();
                     img = this.ConvertBmpToNewImg(bitmap2, encoderInfo, encoderParameters);
                     bitmap2.Dispose();
                     result = img;
                 }
                 else
                 {
                     graphics2.DrawImage(waterImg, new System.Drawing.Rectangle(markx, marky, waterImg.Width, waterImg.Height), 0, 0, waterImg.Width, waterImg.Height, System.Drawing.GraphicsUnit.Pixel, imageAttributes);
                     graphics2.Dispose();
                     img = this.ConvertBmpToNewImg(bitmap2, encoderInfo, encoderParameters);
                     bitmap2.Dispose();
                     result = img;
                 }
             }
             else
             {
                 result = img;
             }
         }
     }
     catch
     {
         result = img;
     }
     return(result);
 }