Esempio n. 1
0
            private void resizePicture(FileInfo file, bool waterMark, Image logo, string type, Int32 width, Int32 height)
            {
                Image       image = Bitmap.FromFile(file.FullName);
                ResizeModel model = new ResizeModel();

                model.Source         = image;
                model.Position       = WatermarkPositions.Center;
                model.IsWartermarked = waterMark;
                if (waterMark && logo != null)
                {
                    model.IsWartermarked  = true;
                    model.ImageWartermark = logo;
                }
                model.ResizeType = ResizeTypes.Absolute;
                model.Width      = width;
                model.Height     = height;
                //DirectoryInfo destDir = file.Directory.CreateSubdirectory(type);
                //string destPath = destDir.FullName + "\\" + file.Name;
                string destPath = GetResizedFilePath(file, type);

                createParentDir(destPath);
                if (!File.Exists(destPath))
                {
                    Image destImage = PictureOperation.Resize(model);
                    PictureOperation.Save(destImage, destPath);
                }
            }
Esempio n. 2
0
        public static Image Resize(Image source, ResizeTypes resizeType, int width, int height)
        {
            ResizeModel model = new ResizeModel();

            model.Source     = source;
            model.ResizeType = resizeType;
            model.Width      = width;
            model.Height     = height;
            return(Resize(model));
        }
Esempio n. 3
0
        private void button_Logo_Click(object sender, EventArgs e)
        {
            /*
             * FileInfo f = new FileInfo("E:\\Personal\\lhl\\input\\img\\default\\Chrysanthemum.jpg");
             * this.label17.Text = this.GetResizedFilePath(f,"large");
             * return;*/
            string      sourceLogo = this.GetDefaultLogoPath("logo.png");
            Image       image      = Bitmap.FromFile(sourceLogo);
            ResizeModel model      = new ResizeModel();

            model.Source         = image;
            model.IsWartermarked = false;
            model.ResizeType     = ResizeTypes.Auto;
            model.Width          = 23;
            model.Height         = 6;
            Image  smallImage = PictureOperation.Resize(model);
            Bitmap pbitmap    = new Bitmap(smallImage);

            pbitmap.MakeTransparent(Color.White);
            string destLogo = this.GetDefaultLogoPath("logo-small.png");

            PictureOperation.Save(pbitmap, destLogo, ImgCodes.PNG, 100);

            image        = Bitmap.FromFile(sourceLogo);
            model.Source = image;
            model.Width  = 138;
            model.Height = 38;
            Image mediumImage = PictureOperation.Resize(model);

            pbitmap = new Bitmap(mediumImage);
            pbitmap.MakeTransparent(Color.White);
            destLogo = this.GetDefaultLogoPath("logo-medium.png");
            PictureOperation.Save(pbitmap, destLogo, ImgCodes.PNG, 100);

            image        = Bitmap.FromFile(sourceLogo);
            model.Source = image;
            model.Width  = 414;
            model.Height = 114;
            Image largeImage = PictureOperation.Resize(model);

            pbitmap = new Bitmap(largeImage);
            pbitmap.MakeTransparent(Color.White);
            destLogo = this.GetDefaultLogoPath("logo-large.png");
            PictureOperation.Save(pbitmap, destLogo, ImgCodes.PNG, 100);
        }
Esempio n. 4
0
        public static Image Resize(ResizeModel model)
        {
            int srcWidth, srcHeight, destWidth, destHeight, cutWidth, cutHeight, x, y;

            #region 初始化变量
            x          = model.StartX;
            y          = model.StartY;
            cutWidth   = srcWidth = model.Source.Width;
            cutHeight  = srcHeight = model.Source.Height;
            destWidth  = model.Width;
            destHeight = model.Height;
            #endregion

            #region 确定画布大小及裁剪尺寸
            switch (model.ResizeType)
            {
            case ResizeTypes.Auto:
                GetFinalSize(model.Width, model.Height, srcWidth, srcHeight, out destWidth, out destHeight);
                break;

            case ResizeTypes.Cut:
                #region 裁剪算法
                double srcProportion  = (double)srcWidth / srcHeight;
                double destProportion = (double)model.Width / model.Height;

                bool autoCut = x == -1 && y == -1;

                if (srcProportion >= destProportion)
                {
                    //高瘦,固定高度,求宽度
                    cutWidth = (int)Math.Floor(srcHeight * destProportion);
                    if (autoCut)
                    {
                        x += (srcWidth - cutWidth) / 2;
                    }
                }
                else
                {
                    //矮胖,固定宽度,球高度
                    cutHeight = (int)Math.Floor(srcWidth / destProportion);
                    if (autoCut)
                    {
                        y += (srcHeight - cutHeight) / 2;
                    }
                }

                if (x + cutWidth > srcWidth)
                {
                    cutWidth = srcWidth - x;
                }

                if (y + cutHeight > srcHeight)
                {
                    cutHeight = srcHeight - y;
                }
                #endregion
                break;

            case ResizeTypes.FixedHeight:
                GetFinalSize(model.Width, model.Height, srcHeight - 1, srcHeight, out destWidth, out destHeight);
                break;

            case ResizeTypes.FixedWidth:
                GetFinalSize(model.Width, model.Height, srcWidth, srcWidth - 1, out destWidth, out destHeight);
                break;

            default:
                break;
            }
            #endregion

            Image imgResult = new Bitmap(destWidth, destHeight);//结果对象

            using (var g = Graphics.FromImage(imgResult))
            {
                g.Clear(Color.White);
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.SmoothingMode     = SmoothingMode.HighQuality;
                //g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                g.CompositingQuality = CompositingQuality.HighQuality;

                Rectangle destRect = new Rectangle(0, 0, destWidth, destHeight);
                Rectangle srcRect  = new Rectangle(x, y, cutWidth, cutHeight);

                g.DrawImage(model.Source, destRect, srcRect, GraphicsUnit.Pixel);

                if (model.IsWartermarked)//是否加水印
                {
                    Image warterMark        = model.ImageWartermark;
                    bool  doImageWartermark = (null != warterMark &&
                                               warterMark.Width < destWidth / 1 &&
                                               warterMark.Height < destHeight / 1);//能否图片水印

                    if (doImageWartermark)
                    {
                        #region 图片水印
                        GetWatermarkPlacement(model.Position, g.DpiX, g.DpiY, destWidth, destHeight, warterMark, out x, out y);

                        /*
                         * ImageAttributes ia = new ImageAttributes();
                         * ColorMatrix cm = new ColorMatrix();
                         * cm.Matrix33 = 0.75f;
                         * ia.SetColorMatrix(cm);
                         */

                        ImageAttributes ia = new ImageAttributes();

                        //The first step in manipulating the watermark image is to replace
                        //the background color with one that is trasparent (Alpha=0, R=0, G=0, B=0)
                        //to do this we will use a Colormap and use this to define a RemapTable
                        ColorMap colorMap = new ColorMap();

                        //My watermark was defined with a background of 100% Green this will
                        //be the color we search for and replace with transparency
                        colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
                        colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);

                        ColorMap[] remapTable = { colorMap };

                        ia.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

                        //The second color manipulation is used to change the opacity of the
                        //watermark.  This is done by applying a 5x5 matrix that contains the
                        //coordinates for the RGBA space.  By setting the 3rd row and 3rd column
                        //to 0.3f we achive a level of opacity
                        float[][] colorMatrixElements =
                        {
                            new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
                            new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f },
                            new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
                            new float[] { 0.0f, 0.0f, 0.0f, 0.3f, 0.0f },
                            new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }
                        };
                        ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);

                        ia.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
                                          ColorAdjustType.Bitmap);


                        g.DrawImage(warterMark, new Rectangle(x, y, warterMark.Width, warterMark.Height), 0, 0, warterMark.Width, warterMark.Height, GraphicsUnit.Pixel, ia);

                        warterMark.Dispose();
                        #endregion
                    }
                    else
                    {
                        #region 文字水印

                        int[] sizes = new int[] { 18, 16, 14, 12, 10, 8, 6, 4 };

                        Font  font = null;
                        SizeF size = new SizeF();

                        for (int i = 0; i < 8; i++)//自动适应大小
                        {
                            font = new Font("arial", sizes[i], FontStyle.Bold);
                            size = g.MeasureString(model.TextWatermark, font);

                            if ((ushort)size.Width < (ushort)destWidth * .90)
                            {
                                break;
                            }
                        }

                        float fx = destWidth * .95f - size.Width;
                        float fy = destHeight * .99f - size.Height;

                        StringFormat StrFormat = new StringFormat();
                        StrFormat.Alignment = StringAlignment.Near;

                        //黑色阴影 (Alpha 153)
                        SolidBrush brush1 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));
                        g.DrawString(model.TextWatermark,
                                     font,
                                     brush1,
                                     new PointF(fx + 1, fy + 1),
                                     StrFormat);

                        //白色前景 (Alpha 153)
                        SolidBrush brush2 = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
                        g.DrawString(model.TextWatermark,
                                     font,
                                     brush2,
                                     new PointF(fx, fy),
                                     StrFormat);
                        #endregion
                    }
                }

                g.Flush();
            }

            model.Source.Dispose();

            return(imgResult);
        }