示例#1
0
        private void OnUpload(UploadEventArgs args)
        {
            UploadEvent handler = MUpload;

            if (handler != null)
            {
                handler(this, args);
            }
        }
示例#2
0
        /// <summary>
        /// 图片等比缩放
        /// </summary>
        /// <param name="postedFile">原图HttpPostedFile对象</param>
        /// <param name="targetWidth">指定的最大宽度</param>
        /// <param name="targetHeight">指定的最大高度</param>
        /// <param name="watermarkText">水印文字(为""表示不使用水印)</param>
        /// <param name="watermarkImage">水印图片路径(为""表示不使用水印)</param>
        public void ZoomAuto(byte[] postedFile, int targetWidth, int targetHeight, string watermarkText, string watermarkImage)
        {
            if (postedFile == null)
            {
                return;
            }

            //原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息)
            var   stream    = new MemoryStream(postedFile);
            Image initImage = Image.FromStream(stream, true);

            //原图宽高均小于模版,不作处理,直接保存
            if (initImage.Width <= targetWidth && initImage.Height <= targetHeight)
            {
                ////生成新图
                ////新建一个bmp图片
                //initImage = new Bitmap(initImage.Width, initImage.Height);
                ////新建一个画板
                //Graphics newG = Graphics.FromImage(initImage);

                ////设置质量
                //newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                //newG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                ////置背景色
                //newG.Clear(Color.White);
                ////画图
                //newG.DrawImage(initImage, new Rectangle(0, 0, initImage.Width, initImage.Height), new Rectangle(0, 0, initImage.Width, initImage.Height), GraphicsUnit.Pixel);
                //生成新图
                //新建一个bmp图片

                Image newImage = new Bitmap(initImage.Width, initImage.Height);
                //新建一个画板
                Graphics newG = Graphics.FromImage(newImage);

                //设置质量
                newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                newG.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                //置背景色
                newG.Clear(Color.White);
                //画图
                newG.DrawImage(initImage, new Rectangle(0, 0, initImage.Width, initImage.Height),
                               new Rectangle(0, 0, initImage.Width, initImage.Height), GraphicsUnit.Pixel);



                //文字水印
                if (watermarkText != "")
                {
                    using (Graphics gWater = Graphics.FromImage(initImage))
                    {
                        var   fontWater  = new Font("黑体", 10);
                        Brush brushWater = new SolidBrush(Color.White);
                        gWater.DrawString(watermarkText, fontWater, brushWater, 10, 10);
                        gWater.Dispose();
                    }
                }

                //透明图片水印
                if (watermarkImage != "")
                {
                    if (File.Exists(watermarkImage))
                    {
                        //获取水印图片
                        using (Image wrImage = GetWatermark(initImage.Width, initImage.Height))
                        {
                            //水印绘制条件:原始图片宽高均大于或等于水印图片
                            if (initImage.Width >= wrImage.Width && initImage.Height >= wrImage.Height)
                            {
                                Graphics gWater = Graphics.FromImage(initImage);

                                //透明属性
                                var imgAttributes = new ImageAttributes();
                                //var colorMap = new ColorMap
                                //                   {
                                //                       OldColor = Color.FromArgb(0, 0, 0, 0),
                                //                       NewColor = Color.FromArgb(0, 0, 0, 0)
                                //                   };
                                //ColorMap[] remapTable = { colorMap };
                                //imgAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

                                float[][] colorMatrixElements =
                                {
                                    new[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
                                    new[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f },
                                    new[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
                                    new[] { 0.0f, 0.0f, 0.0f, 0.1f, 0.0f },
                                    new[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }
                                };
                                var wmColorMatrix = new ColorMatrix(colorMatrixElements);
                                imgAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                                gWater.DrawImage(wrImage, new Rectangle(initImage.Width - wrImage.Width, initImage.Height - wrImage.Height, wrImage.Width, wrImage.Height), 0, 0, wrImage.Width, wrImage.Height, GraphicsUnit.Pixel, imgAttributes);

                                gWater.Dispose();
                            }
                            wrImage.Dispose();
                        }
                    }
                }
                var args = new UploadEventArgs {
                    UploadImage = initImage, FileName = FileName, MidFilepath = MidFilepath
                };
                OnUpload(args);
                //保存
                SavePath = args.SaveFileName;
                if (!string.IsNullOrWhiteSpace(SavePath))
                {
                    if (File.Exists(SavePath))
                    {
                        File.Delete(SavePath);
                    }
                    initImage.Save(SavePath, ImageFormat.Jpeg);
                }
            }
            else
            {
                //缩略图宽、高计算
                int newWidth  = initImage.Width;
                int newHeight = initImage.Height;

                //宽大于高或宽等于高(横图或正方)
                if (initImage.Width > initImage.Height || initImage.Width == initImage.Height)
                {
                    //如果宽大于模版
                    if (initImage.Width > targetWidth)
                    {
                        //宽按模版,高按比例缩放
                        newWidth = targetWidth;
                        var height = initImage.Height * (targetWidth / Convert.ToDouble(initImage.Width));
                        newHeight = (int)height;
                    }
                }
                //高大于宽(竖图)
                else
                {
                    //如果高大于模版
                    if (initImage.Height > targetHeight)
                    {
                        //高按模版,宽按比例缩放
                        newHeight = targetHeight;
                        var width = initImage.Width * (targetHeight / initImage.Height);
                        newWidth = width;
                    }
                }

                //生成新图
                //新建一个bmp图片
                Image newImage = new Bitmap(newWidth, newHeight);
                //新建一个画板
                Graphics newG = Graphics.FromImage(newImage);

                //设置质量
                newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                newG.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                //置背景色
                newG.Clear(Color.White);
                //画图
                newG.DrawImage(initImage, new Rectangle(0, 0, newImage.Width, newImage.Height), new Rectangle(0, 0, initImage.Width, initImage.Height), GraphicsUnit.Pixel);

                //文字水印
                if (watermarkText != "")
                {
                    using (var gWater = Graphics.FromImage(newImage))
                    {
                        var fontWater  = new Font("宋体", 10);
                        var brushWater = new SolidBrush(Color.White);
                        gWater.DrawString(watermarkText, fontWater, brushWater, 10, 10);
                        gWater.Dispose();
                    }
                }

                //透明图片水印
                if (watermarkImage != "")
                {
                    if (File.Exists(watermarkImage))
                    {
                        //获取水印图片
                        using (Image wrImage = GetWatermark(newWidth, newHeight))
                        {
                            //水印绘制条件:原始图片宽高均大于或等于水印图片
                            if (newImage.Width >= wrImage.Width && newImage.Height >= wrImage.Height)
                            {
                                Graphics gWater = Graphics.FromImage(newImage);

                                //透明属性
                                var imgAttributes = new ImageAttributes();
                                //ColorMap colorMap = new ColorMap();
                                //colorMap.OldColor = Color.FromArgb(0, 0, 0, 0);
                                //colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
                                //ColorMap[] remapTable = { colorMap };
                                //imgAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);


                                float[][] colorMatrixElements =
                                {
                                    new[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
                                    new[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f },
                                    new[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
                                    new[] { 0.0f, 0.0f, 0.0f, 0.1f, 0.0f },
                                    new[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }
                                };

                                var wmColorMatrix = new ColorMatrix(colorMatrixElements);
                                imgAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                                gWater.DrawImage(wrImage, new Rectangle(newImage.Width - wrImage.Width, newImage.Height - wrImage.Height, wrImage.Width, wrImage.Height), 0, 0, wrImage.Width, wrImage.Height, GraphicsUnit.Pixel, imgAttributes);
                                gWater.Dispose();
                            }
                            wrImage.Dispose();
                        }
                    }
                }
                stream.Close();
                //保存缩略图
                var args = new UploadEventArgs {
                    UploadImage = newImage, FileName = FileName, MidFilepath = MidFilepath
                };
                OnUpload(args);
                SavePath = args.SaveFileName;
                if (!string.IsNullOrWhiteSpace(SavePath))
                {
                    if (File.Exists(SavePath))
                    {
                        File.Delete(SavePath);
                    }
                    newImage.Save(SavePath, ImageFormat.Jpeg);
                }

                //释放资源
                newG.Dispose();
                newImage.Dispose();
                initImage.Dispose();
            }
        }