Byte[]到image对象,画水印防盗链,到MemoryStream

来源:互联网 发布:js length 中文 编辑:程序博客网 时间:2024/05/16 05:05

最近在写一个电子商城,需要用到防盗链图片,所以就把图片作为Byte[]存到数据库中,用到的时候再转换成IMAGE对象,通过以下方法画水印,防盗链的方法应该在页面基类中控制,这里只是写一下从Byte[]到到图片流的输出

 

类:

 

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Text;
using System.IO;
using Shop.Business.Sys;
using Shop.Common;

namespace Shop.Business.Common
{
    /// <summary>
    /// 水印图片对象类
    /// </summary>
    public class WaterImage
    {
        public WaterImage()
        {

        }

        private Image m_sourcePicture;
        /// <summary>
        /// 源图片
        /// </summary>
        public Image SourcePicture
        {
            get { return m_sourcePicture; }
            set { m_sourcePicture = value; }
        }

        /// <summary>
        /// 得到水印文字的内容
        /// </summary>
        public string TxtStr
        {
            get
            {
                return SysConfigs.GetWebSiteWaterMarkTxtStr;
            }
        }

        /// <summary>
        /// 得到水印文字大小
        /// </summary>
        public int TxtFontSize
        {
            get
            {
                return SysConfigs.GetWebSiteWaterMarkTxtFontSize;
            }
        }

        /// <summary>
        /// 得到水印文字颜色
        /// </summary>
        public string TxtFontColor
        {
            get
            {
                return SysConfigs.GetWebSiteWaterMarkTxtFontColor;
            }
        }
        /// <summary>
        /// 得到水印字体名称
        /// </summary>
        public string TxtFontFamily
        {
            get
            {
                return SysConfigs.GetWebSiteWaterMarkTxtFontFamily;
            }
        }


        /// <summary>
        /// 得到水印字体是否粗体
        /// </summary>
        public bool TxtStrong
        {
            get
            {
                return SysConfigs.GetWebSiteWaterMarkTxtStrong;
            }
        }

        /// <summary>
        /// 得到水印LOGO图片
        /// </summary>
        public string Image
        {
            get
            {
                return SysConfigs.GetWebSiteWaterMarkImage;
            }
        }

        /// <summary>
        /// 得到水印透明度
        /// </summary>
        public double ImageTransparence
        {
            get
            {
                return SysConfigs.GetWebSiteWaterMarkImageTransparence;
            }
        }

        /// <summary>
        /// 得到水印图片去除底色 58(为0,则不去底色)
        /// </summary>
        public string ImageWipeOffBackColor
        {
            get
            {
                return SysConfigs.GetWebSiteWaterMarkImageWipeOffBackColor;
            }
        }

        /// <summary>
        /// 得到水印文字/LOGO位置
        /// </summary>
        public ImagePosition ImageAlgin
        {
            get
            {
                return SysConfigs.GetWebSiteWaterMarkImageAlgin;
            }
        }


        /// <summary>
        /// 得到水印文字/LOGO长度 60
        /// </summary>
        public int ImageWidth
        {
            get
            {
                return SysConfigs.GetWebSiteWaterMarkImageWidth;
            }
        }

 

        /// <summary>
        /// 得到水印文字/LOGO高度 61
        /// </summary>
        public int ImageHeight
        {
            get
            {
                return SysConfigs.GetWebSiteWaterMarkImageHeight;
            }
        }

 

    }
    /// <summary>
    /// 图片位置
    /// </summary>
    public enum ImagePosition
    {
        /// <summary>
        /// 左上0
        /// </summary>
        LeftTop,
        /// <summary>
        /// 左下1
        /// </summary>
        LeftBottom,
        /// <summary>
        /// 右上2
        /// </summary>
        RightTop,
        /// <summary>
        /// 右下3
        /// </summary>
        RigthBottom,
        /// <summary>
        /// 顶部居中4
        /// </summary>
        TopMiddle,
        /// <summary>
        /// 底部居中5
        /// </summary>
        BottomMiddle,
        /// <summary>
        /// 中心6
        /// </summary>
        Center
    }

    /// <summary>
    /// 水印图片管理类
    /// </summary>
    public class WaterImageManage
    {
        /// <summary>
        /// 生成一个新的水印图片制作实例
        /// </summary>
        public WaterImageManage()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public MemoryStream DrawImage(WaterImage waterImage, int width, int height)
        {
            try
            {
                MemoryStream ms = new MemoryStream();
                if (waterImage == null || waterImage.SourcePicture == null)
                {
                    return null;
                }

               
                ms.Position = 0;
                waterImage.SourcePicture.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                Image imgPhoto = waterImage.SourcePicture;
                //原图片的宽度
                int spWidth = imgPhoto.Width;
                //生成图片的宽度
                int ciWidth = spWidth;


                if (width > 0)
                {
                    ciWidth = width;
                }
                 
               
                //原图片的高度
                int spHeight = imgPhoto.Height;
                //生成图片的高度
                int ciHeight = spHeight;
                if (height > 0)
                {
                    ciHeight = height;
                }
               

 

                #region 水印


                //
                // 与底图一样,我们需要一个位图来装载水印图片。并设定其分辨率
                //
                if (!string.IsNullOrEmpty(waterImage.Image))
                {
                    //
                    // 封装 GDI+ 位图,此位图由图形图像及其属性的像素数据组成。
                    //
                    Bitmap ciPhoto = new Bitmap(ciWidth, ciHeight, PixelFormat.Format24bppRgb);

                    ciPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
                    //
                    // 定义一个绘图画面用来装载位图
                    //
                    Graphics grPhoto = Graphics.FromImage(ciPhoto);

                    //设置高质量插值法
                    grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

                  


                    //SmoothingMode:指定是否将平滑处理(消除锯齿)应用于直线、曲线和已填充区域的边缘。
                    // 成员名称   说明
                    // AntiAlias      指定消除锯齿的呈现。 
                    // Default        指定不消除锯齿。 
                    // HighQuality    指定高质量、低速度呈现。 
                    // HighSpeed      指定高速度、低质量呈现。 
                    // Invalid        指定一个无效模式。 
                    // None           指定不消除锯齿。
                    grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
                    grPhoto.Clear(Color.Transparent);


                    //
                    //同样,由于水印是图片,我们也需要定义一个Image来装载它
                    //
                    Image imgWatermark = new Bitmap(Function.PhysicalApplicationPath + @"/" + waterImage.Image);
                    if (imgWatermark != null)
                    {
                    //
                    // 获取水印图片的高度和宽度
                    //
                    int wmWidth = imgWatermark.Width;
                    if (waterImage.ImageWidth > 0)
                    {
                        wmWidth = waterImage.ImageWidth;
                    }
                   
                    int wmHeight = imgWatermark.Height;
                    if (waterImage.ImageHeight > 0)
                    {
                        wmHeight = waterImage.ImageHeight;
                    }

                  

                    //
                    // 第一次描绘,将我们的底图描绘在绘图画面上
                    //
                    grPhoto.DrawImage(imgPhoto,
                                       new Rectangle(0, 0, ciWidth, ciHeight),
                                       new Rectangle(0, 0, spWidth, spHeight),
                                       GraphicsUnit.Pixel);
                    //
                    // 与底图一样,我们需要一个位图来装载水印图片。并设定其分辨率
                    //
                    Bitmap bmWatermark = new Bitmap(ciPhoto);
                   
                        bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
                        if (waterImage.ImageWipeOffBackColor != "0")
                        {
                            string rgb = waterImage.ImageWipeOffBackColor;
                            int r = Convert.ToInt16("0x" + rgb.Substring(1, 2), 16);
                            int g = Convert.ToInt16("0x" + rgb.Substring(3, 2), 16);
                            int b = Convert.ToInt16("0x" + rgb.Substring(5, 2), 16);
                            //SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(m_alpha, r, g, b));
                            bmWatermark.MakeTransparent(Color.FromArgb( r, g, b));
                        }

                       

                       

                        //
                        // 继续,将水印图片装载到一个绘图画面grWatermark
                        //
                        Graphics grWatermark = Graphics.FromImage(bmWatermark);

                        //
                        //ImageAttributes 对象包含有关在呈现时如何操作位图和图元文件颜色的信息。
                        //      
                        ImageAttributes imageAttributes = new ImageAttributes();

                        //
                        //Colormap: 定义转换颜色的映射
                        //
                        ColorMap colorMap = new ColorMap();
                        if (waterImage.ImageWipeOffBackColor != "0")
                        {
                            string rgb = waterImage.ImageWipeOffBackColor;
                            int r = Convert.ToInt16("0x" + rgb.Substring(1, 2), 16);
                            int g = Convert.ToInt16("0x" + rgb.Substring(3, 2), 16);
                            int b = Convert.ToInt16("0x" + rgb.Substring(5, 2), 16);
                            colorMap.NewColor = Color.FromArgb(r, g, b);
                        }

                        ColorMap[] remapTable = { colorMap };

                        imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
                        float[][] colorMatrixElements = {
           new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f}, // red红色
           new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f}, //green绿色
           new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f}, //blue蓝色      
           new float[] {0.0f,  0.0f,  0.0f,  float.Parse(waterImage.ImageTransparence.ToString()), 0.0f}, //透明度
            //new float[] {float.Parse(waterImage.ImageTransparence.ToString()),  float.Parse(waterImage.ImageTransparence.ToString()),  float.Parse(waterImage.ImageTransparence.ToString()),  float.Parse(waterImage.ImageTransparence.ToString()), float.Parse(waterImage.ImageTransparence.ToString())}, //透明度
           new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}};//

                        //  ColorMatrix:定义包含 RGBA 空间坐标的 5 x 5 矩阵。
                        //  ImageAttributes 类的若干方法通过使用颜色矩阵调整图像颜色。
                        ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);

 

                        imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
                         ColorAdjustType.Bitmap);
                        //
                        //上面设置完颜色,下面开始设置位置
                        //
                        int xPosOfWm;
                        int yPosOfWm;

                        switch (waterImage.ImageAlgin)
                        {
                            case ImagePosition.BottomMiddle:
                                xPosOfWm = (ciWidth - wmWidth) / 2;
                                yPosOfWm = ciHeight - wmHeight - 10;
                                break;
                            case ImagePosition.Center:
                                xPosOfWm = (ciWidth - wmWidth) / 2;
                                yPosOfWm = (ciHeight - wmHeight) / 2;
                                break;
                            case ImagePosition.LeftBottom:
                                xPosOfWm = 10;
                                yPosOfWm = ciHeight - wmHeight - 10;
                                break;
                            case ImagePosition.LeftTop:
                                xPosOfWm = 10;
                                yPosOfWm = 10;
                                break;
                            case ImagePosition.RightTop:
                                xPosOfWm = ciWidth - wmWidth - 10;
                                yPosOfWm = 10;
                                break;
                            case ImagePosition.RigthBottom:
                                xPosOfWm = ciWidth - wmWidth - 10;
                                yPosOfWm = ciHeight - wmHeight - 10;
                                break;
                            case ImagePosition.TopMiddle:
                                xPosOfWm = (ciWidth - wmWidth) / 2;
                                yPosOfWm = 10;
                                break;
                            default:
                                xPosOfWm = 10;
                                yPosOfWm = ciHeight - wmHeight - 10;
                                break;
                        }

                        //
                        // 第二次绘图,把水印印上去
                        //
                        grWatermark.DrawImage(imgWatermark,
                         new Rectangle(xPosOfWm,
                                             yPosOfWm,
                                             wmWidth,
                                             wmHeight),
                                           0, 0, imgWatermark.Width, imgWatermark.Height,
                                             GraphicsUnit.Pixel,
                                             imageAttributes);


                        grPhoto.Dispose();
                        grWatermark.Dispose();
                        imgPhoto = bmWatermark;
                        //
                        // 保存文件到服务器的文件夹里面
                        //
                        ms.Position = 0;
                        imgPhoto.Save(ms, ImageFormat.Gif);
                        //imgPhoto.Dispose();
                        imgWatermark.Dispose();

                    }
                }
 
                #endregion

                    #region 文字
               
                    if (!string.IsNullOrEmpty(waterImage.TxtStr))
                    {
                        Bitmap ciPhoto = new Bitmap(ciWidth, ciHeight, PixelFormat.Format24bppRgb);

                        //ciPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
                        //Graphics:封装一个 GDI+ 绘图图面。
                        Graphics grPhoto = Graphics.FromImage(ciPhoto);

                        //设置高质量插值法
                        grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

 


                        //SmoothingMode:指定是否将平滑处理(消除锯齿)应用于直线、曲线和已填充区域的边缘。
                        // 成员名称   说明
                        // AntiAlias      指定消除锯齿的呈现。 
                        // Default        指定不消除锯齿。 
                        // HighQuality    指定高质量、低速度呈现。 
                        // HighSpeed      指定高速度、低质量呈现。 
                        // Invalid        指定一个无效模式。 
                        // None           指定不消除锯齿。
                        grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
                        grPhoto.Clear(Color.Transparent);

                        //将我们要添加水印的图片按照原始大小描绘(复制)到图形中
                        grPhoto.DrawImage(
                         imgPhoto,                                           //   要添加水印的图片
                         new Rectangle(0, 0, ciWidth, ciHeight), //  根据要添加的水印图片的宽和高
                         0,                                                     //  X方向从0点开始描绘
                         0,                                                     // Y方向
                         ciWidth,                                            //  X方向描绘长度
                         ciHeight,                                           //  Y方向描绘长度
                         GraphicsUnit.Pixel);                              // 描绘的单位,这里用的是像素

                        //根据图片的大小我们来确定添加上去的文字的大小

                        //字体
                        Font crFont = null;
                        //矩形的宽度和高度,SizeF有三个属性,分别为Height高,width宽,IsEmpty是否为空
                        SizeF crSize = new SizeF();
                        if (waterImage.TxtStrong)
                        {
                            crFont = new Font(waterImage.TxtFontFamily, waterImage.TxtFontSize, FontStyle.Bold);
                        }
                        else
                        {
                            crFont = new Font(waterImage.TxtFontFamily, waterImage.TxtFontSize, FontStyle.Regular);
                        }

                       

                        //测量用指定的 Font 对象绘制并用指定的 StringFormat 对象格式化的指定字符串。
                        crSize = grPhoto.MeasureString(waterImage.TxtStr, crFont);

                        //截边5%的距离,定义文字显示(由于不同的图片显示的高和宽不同,所以按百分比截取)
                        int yPixlesFromBottom = (int)(ciHeight * .05);

                        //定义在图片上文字的位置
                        float wmHeight = crSize.Height;
                        float wmWidth = crSize.Width;

                        float xPosOfWm;
                        float yPosOfWm;

                        switch (waterImage.ImageAlgin)
                        {
                            case ImagePosition.BottomMiddle:
                                xPosOfWm = ciWidth  / 2;
                                yPosOfWm = ciHeight - wmHeight - 10;
                                break;
                            case ImagePosition.Center:
                                xPosOfWm = ciWidth  / 2;
                                yPosOfWm = ciHeight / 2;
                                break;
                            case ImagePosition.LeftBottom:
                                xPosOfWm = 10 + wmWidth / 2;
                                yPosOfWm = ciHeight - wmHeight - 10;
                                break;
                            case ImagePosition.LeftTop:
                                xPosOfWm = 10 + wmWidth / 2;
                                yPosOfWm = 10 + wmHeight / 2;
                                break;
                            case ImagePosition.RightTop:
                                xPosOfWm = ciWidth - (10 + wmWidth / 2);
                                yPosOfWm = 10;
                                break;
                            case ImagePosition.RigthBottom:
                                xPosOfWm = ciWidth - (10 + wmWidth/2);
                                yPosOfWm = ciHeight - wmHeight - 10;
                                break;
                            case ImagePosition.TopMiddle:
                                xPosOfWm = ciWidth / 2;
                                yPosOfWm = 10;
                                break;
                            default:
                                xPosOfWm = 10;
                                yPosOfWm = ciHeight - wmHeight - 10;
                                break;
                        }

                        //封装文本布局信息(如对齐、文字方向和 Tab 停靠位),显示操作(如省略号插入和国家标准 (National) 数字替换)和 OpenType 功能。
                        StringFormat StrFormat = new StringFormat();

                        //定义需要印的文字居中对齐
                        StrFormat.Alignment = StringAlignment.Center;

                        //SolidBrush:定义单色画笔。画笔用于填充图形形状,如矩形、椭圆、扇形、多边形和封闭路径。
                        //这个画笔为描绘阴影的画笔,呈灰色
                        int m_alpha = Convert.ToInt32(256 * waterImage.ImageTransparence);
                        SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(m_alpha, 0, 0, 0));

                        //描绘文字信息,这个图层向右和向下偏移一个像素,表示阴影效果
                        //DrawString 在指定矩形并且用指定的 Brush 和 Font 对象绘制指定的文本字符串。
                        grPhoto.DrawString(waterImage.TxtStr,                                    //string of text
                                                   crFont,                                         //font
                                                   semiTransBrush2,                            //Brush
                                                   new PointF(xPosOfWm + 1, yPosOfWm + 1),  //Position
                                                   StrFormat);

                        //从四个 ARGB 分量(alpha、红色、绿色和蓝色)值创建 Color 结构,这里设置透明度为153
                        //这个画笔为描绘正式文字的笔刷,呈白色
                       
                         string rgb = waterImage.TxtFontColor;
                            int r = Convert.ToInt16("0x" + rgb.Substring(1, 2), 16);
                            int g = Convert.ToInt16("0x" + rgb.Substring(3, 2), 16);
                            int b = Convert.ToInt16("0x" + rgb.Substring(5, 2), 16);
                            SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(m_alpha, r, g, b));

                        //第二次绘制这个图形,建立在第一次描绘的基础上
                            grPhoto.DrawString(waterImage.TxtStr,                 //string of text
                                                   crFont,                                   //font
                                                   semiTransBrush,                           //Brush
                                                   new PointF(xPosOfWm, yPosOfWm),  //Position
                                                   StrFormat);

                        //imgPhoto是我们建立的用来装载最终图形的Image对象
                        //bmPhoto是我们用来制作图形的容器,为Bitmap对象
                        imgPhoto = ciPhoto;
                        //释放资源,将定义的Graphics实例grPhoto释放,grPhoto功德圆满
                        grPhoto.Dispose();

                        //将grPhoto保存
                        ms.Position = 0;
                        imgPhoto.Save(ms, ImageFormat.Gif);
                       
                    }
                
                    #endregion
                    imgPhoto.Dispose();

                return ms;

            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }

    /// <summary>
    /// 图片管理类
    /// </summary>
    public class ImageManage
    {
        /// <summary>
        ///
        /// </summary>
        public ImageManage()
        {
            //
            //TODO:
            //
        }

        /// <summary>
        /// 画图片
        /// </summary>
        /// <param name="waterImage"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public MemoryStream DrawImage(WaterImage waterImage, int width, int height)
        {
            MemoryStream ms = new MemoryStream();
            try
            {


                if (waterImage != null && waterImage.SourcePicture != null)
                {
                    ms = new WaterImageManage().DrawImage(waterImage, width, height);
                }
                if (ms == null||ms.Length==0)
                {
                    throw new Exception();
                }

            }
            catch
            {
                string errStr = "对不起,未找到图片或生成图片出错!";
                //图片的宽度
                int ciWidth = 400;


                if (width > 0)
                {
                    ciWidth = width;
                }

 

                //图片的高度
                int ciHeight = 400;
                if (height > 0)
                {
                    ciHeight = height;
                }
                Bitmap errimage = new Bitmap(ciWidth, ciHeight, PixelFormat.Format24bppRgb);
                errimage.MakeTransparent();
                //errimage.SetResolution(float.Parse(ciWidth.ToString()), float.Parse(ciHeight.ToString()));
                Graphics graphics = Graphics.FromImage(errimage);
              

                //设置高质量插值法
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                //SmoothingMode:指定是否将平滑处理(消除锯齿)应用于直线、曲线和已填充区域的边缘。
                // 成员名称   说明
                // AntiAlias      指定消除锯齿的呈现。 
                // Default        指定不消除锯齿。 
                // HighQuality    指定高质量、低速度呈现。 
                // HighSpeed      指定高速度、低质量呈现。 
                // Invalid        指定一个无效模式。 
                // None           指定不消除锯齿。
                graphics.SmoothingMode = SmoothingMode.AntiAlias;
                graphics.Clear(Color.FromArgb(255, Color.Transparent));
               


               
                //字体
                Font crFont = null;
                //矩形的宽度和高度,SizeF有三个属性,分别为Height高,width宽,IsEmpty是否为空
                SizeF crSize = new SizeF();

                crFont = new Font("宋体", 12, FontStyle.Regular);

                //测量用指定的 Font 对象绘制并用指定的 StringFormat 对象格式化的指定字符串。
                crSize = graphics.MeasureString(errStr, crFont);

                //截边5%的距离,定义文字显示(由于不同的图片显示的高和宽不同,所以按百分比截取)
                int yPixlesFromBottom = (int)(ciHeight * .05);

                //定义在图片上文字的位置
                float wmHeight = crSize.Height;
                float wmWidth = crSize.Width;

                float xPosOfWm = ciWidth / 2;
                float yPosOfWm = ciHeight / 2;


                //封装文本布局信息(如对齐、文字方向和 Tab 停靠位),显示操作(如省略号插入和国家标准 (National) 数字替换)和 OpenType 功能。
                StringFormat StrFormat = new StringFormat();

                //定义需要印的文字居中对齐
                StrFormat.Alignment = StringAlignment.Center;

                //SolidBrush:定义单色画笔。画笔用于填充图形形状,如矩形、椭圆、扇形、多边形和封闭路径。
                //这个画笔为描绘阴影的画笔,呈灰色
                int m_alpha = Convert.ToInt32(255 * 1);

                string rgb = "#666666";
                int r = Convert.ToInt16("0x" + rgb.Substring(1, 2), 16);
                int g = Convert.ToInt16("0x" + rgb.Substring(3, 2), 16);
                int b = Convert.ToInt16("0x" + rgb.Substring(5, 2), 16);
                SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(m_alpha, r, g, b));

                //第二次绘制这个图形,建立在第一次描绘的基础上
                graphics.DrawString(errStr,                 //string of text
                                       crFont,                                   //font
                                       semiTransBrush,                           //Brush
                                       new PointF(xPosOfWm, yPosOfWm),  //Position
                                       StrFormat);
                errimage.Save(ms, ImageFormat.Gif);
            }
           
                return ms;
           
        }
    }
}

存在的问题:本人想把类ImageManage.DrawImage的catch中画出的图片背景弄成透明,但是没有实现...期望高手看了指点一二

 

页面:

public partial class CommonPage_CreateImage : BasePage
{
    #region 页面参数

    /// <summary>
    /// 页面参数这里通过反射来创建参数值取imagesbyte的对象

    /// </summary>
    protected ImagesByte imagesByte
    {
        get
        {
            string qs = "imagesbyte";//参数名称
            return GetQueryStringReturnObj(typeof(ImagesByte), qs) as ImagesByte;
        }
    }

    /// <summary>
    /// 图片宽度
    /// </summary>
    protected int Width
    {
        get
        {
            if (Request.QueryString["w"] == null || string.IsNullOrEmpty(Request.QueryString["w"].ToString()))
            {
                return -1;
            }
            else
            {
                try
                {
                    return int.Parse(Request.QueryString["w"].ToString());
                }
                catch
                {
                    return -1;
                }
            }
        }
    }

    /// <summary>
    /// 图片高度
    /// </summary>
    protected int Height
    {
        get
        {
            if (Request.QueryString["h"] == null || string.IsNullOrEmpty(Request.QueryString["h"].ToString()))
            {
                return -1;
            }
            else
            {
                try
                {
                    return int.Parse(Request.QueryString["h"].ToString());
                }
                catch
                {
                    return -1;
                }
            }
        }
    }

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            WaterImage wi = new WaterImage();
            if (this.imagesByte != null)
            {
                wi.SourcePicture = this.imagesByte.GetImage

            }
                          
                System.IO.MemoryStream ms = new ImageManage().DrawImage(wi, Width, Height);
                if (ms == null)
                {
                    throw new Exception("生成图片出错");
                }
               // System.IO.MemoryStream ms = new System.IO.MemoryStream(this.imagesByte.Byte);
                HttpContext.Current.Response.ContentType = "image/Jpeg";
                HttpContext.Current.Response.BinaryWrite(ms.ToArray());

           
        }
        catch (Exception ex)
        {
            //Response.Write(ex.Message);
        }
       
    }
}

 

使用:

<img src="CreateImage.aspx?imagesbyte=1" />

原创粉丝点击