Пример #1
0
        /// <summary>
        /// 开始绘制文字水印
        /// </summary>
        /// <example>
        /// <code>
        /// Watermark wm = new Watermark();
        /// wm.ModifyImagePath=path;
        /// wm.OutPath=Server.MapPath("") + "/upfile/" + fileName + "_new" + extension;
        /// wm.DrawText("测试", 1, 100);
        /// </code>
        /// </example>
        /// <param name="watermarkText">文字</param>
        /// <param name="watermarkStatus">1-9</param>
        /// <param name="quality">质量</param>
        /// <param name="fontname">字体</param>
        /// <param name="fontsize">大小</param>
        public void DrawText(string watermarkText, int watermarkStatus, int quality, string fontname = "宋体", int fontsize = 12)
        {
            if (!FileDirectory.FileExists(this.ModifyImagePath))
            {
                return;
            }

            Image  img      = Image.FromFile(this.ModifyImagePath);
            string filename = this.OutPath.IsNullEmpty() ? this.ModifyImagePath : this.OutPath;

            Graphics g        = Graphics.FromImage(img);
            Font     drawFont = new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel);
            SizeF    crSize;

            crSize = g.MeasureString(watermarkText, drawFont);

            float xpos = 0;
            float ypos = 0;

            switch (watermarkStatus)
            {
            case 1:
                xpos = (float)img.Width * (float).01;
                ypos = (float)img.Height * (float).01;
                break;

            case 2:
                xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
                ypos = (float)img.Height * (float).01;
                break;

            case 3:
                xpos = ((float)img.Width * (float).99) - crSize.Width;
                ypos = (float)img.Height * (float).01;
                break;

            case 4:
                xpos = (float)img.Width * (float).01;
                ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
                break;

            case 5:
                xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
                ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
                break;

            case 6:
                xpos = ((float)img.Width * (float).99) - crSize.Width;
                ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);
                break;

            case 7:
                xpos = (float)img.Width * (float).01;
                ypos = ((float)img.Height * (float).99) - crSize.Height;
                break;

            case 8:
                xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);
                ypos = ((float)img.Height * (float).99) - crSize.Height;
                break;

            case 9:
                xpos = ((float)img.Width * (float).99) - crSize.Width;
                ypos = ((float)img.Height * (float).99) - crSize.Height;
                break;
            }

            g.DrawString(watermarkText, drawFont, new SolidBrush(Color.White), xpos + 1, ypos + 1);
            g.DrawString(watermarkText, drawFont, new SolidBrush(Color.Black), xpos, ypos);

            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo   ici    = null;

            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.MimeType.IndexOf("jpeg") > -1)
                {
                    ici = codec;
                }
            }
            EncoderParameters encoderParams = new EncoderParameters();

            long[] qualityParam = new long[1];
            if (quality < 0 || quality > 100)
            {
                quality = 80;
            }

            qualityParam[0] = quality;

            EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);

            encoderParams.Param[0] = encoderParam;

            if (FileDirectory.FileExists(this.ModifyImagePath))
            {
                FileDirectory.FileDelete(this.ModifyImagePath);
            }
            if (ici.IsNotNull())
            {
                img.Save(filename, ici, encoderParams);
            }
            else
            {
                img.Save(filename);
            }

            g.Dispose();
            img.Dispose();
        }
Пример #2
0
        /// <summary>
        /// 按宽X长比例切图
        /// </summary>
        /// <param name="imagePath">源图地址</param>
        /// <param name="savePath">新图地址</param>
        /// <param name="cutWidth">宽度</param>
        /// <param name="cutHeight">高度</param>
        /// <param name="error">出错时执行</param>
        /// <returns>成功true失败false</returns>
        public static bool CutImageCustom(string imagePath, string savePath, int cutWidth, int cutHeight, Action <Exception> error = null)
        {
            try {
                System.Drawing.Image objImage = System.Drawing.Image.FromFile(imagePath);
                float x = objImage.Width;
                float y = objImage.Height;

                float xPercent = x / cutWidth;
                float yPercent = y / cutHeight;

                int width = 0, height = 0;
                if (xPercent < yPercent)
                {
                    width  = (int)((x * cutHeight) / y);
                    height = cutHeight;
                }
                else
                {
                    width  = cutWidth;
                    height = (int)((cutWidth * y) / x);
                }

                Bitmap newimage = new Bitmap(width, height, PixelFormat.Format32bppRgb);
                newimage.SetResolution(72f, 72f);
                Graphics gdiobj = Graphics.FromImage(newimage);
                gdiobj.CompositingQuality = CompositingQuality.HighQuality;
                gdiobj.SmoothingMode      = SmoothingMode.HighQuality;
                gdiobj.InterpolationMode  = InterpolationMode.HighQualityBicubic;
#if !MONO40
                gdiobj.PixelOffsetMode = PixelOffsetMode.HighQuality;
#endif
                gdiobj.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);
                Rectangle destrect = new Rectangle(0, 0, width, height);
                gdiobj.DrawImage(objImage, destrect, 0, 0, objImage.Width, objImage.Height, GraphicsUnit.Pixel);
                gdiobj.Dispose();

                System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters(1);
                ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100);

                ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo   ici    = null;
                foreach (ImageCodecInfo codec in codecs)
                {
                    if (codec.MimeType == "image/jpeg")
                    {
                        ici = codec;
                    }
                }

                if (ici.IsNotNull())
                {
                    newimage.Save(savePath, ici, ep);
                }
                else
                {
                    newimage.Save(savePath, objImage.RawFormat);
                }

                objImage.Dispose();
                newimage.Dispose();

                FileStream fs = new FileStream(savePath, FileMode.Open, FileAccess.Read);
                fs.Close();

                return(true);
            } catch (Exception ex) {
                if (error.IsNotNull())
                {
                    error(ex);
                }
                return(false);
            }
        }