图片上传函数,进行判断是否加水印,做出两种处理方式: 
  /**//// <summary>        
/// 上传图片代码 
  /// </summary>   
/// <param name="image_file">HtmlInputFile控件</param> 
   /// <param name="ImgPath">存放的文件夹绝对位置</param>   
 /// <param name="ImgLink">生成的图片的名称带后缀</param>     
/// <returns></returns> 
     public bool UpPic(System.Web.UI.HtmlControls.HtmlInputFile image_file, string ImgPath, string ImgLink) 
{ 
    if (image_file.PostedFile.FileName != null && image_file.PostedFile.FileName.Trim() != "") 
    { 
        try 
        { 
            if (!System.IO.Directory.Exists(ImgPath)) 
            { 
                System.IO.Directory.CreateDirectory(ImgPath); 
            } 
            //生成缩略图 
            this.GreateMiniImage((ImgPath + "\\" + "old_" + ImgLink), (ImgPath + "\\" + "mini_" + ImgLink), 50, 50); 
            //如果显示水印 
            if (ShowWatermark) 
            { 
                image_file.PostedFile.SaveAs(ImgPath + "\\" + "old_" + ImgLink); 
                //加水印 
                this.addWaterMark((ImgPath + "\\" + "old_" + ImgLink), (ImgPath + "\\" + ImgLink)); 
            } 
            else 
            { 
                image_file.PostedFile.SaveAs(ImgPath + "\\" + ImgLink); 
            } 
            return true; 
        } 
        catch 
        { 
            return false; 
        } 
    } 
    else 
    { 
        return false; 
    } 
} 
 
加水印的函数如下: 
   填加图片函数,需要下面两个函数的支持,当然也可以写到一起,不过那看起来就很冗长了。 
        /**//// <summary> 
           /// 添加图片水印 
           /// </summary> 
            /// <param name="oldpath">原图片绝对地址</param> 
            /// <param name="newpath">新图片放置的绝对地址</param> 
        private void addWaterMark(string oldpath, string newpath) 
{ 
    try 
    { 
        System.Drawing.Image image = System.Drawing.Image.FromFile(oldpath); 
        Bitmap b = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb); 
        Graphics g = Graphics.FromImage(b); 
        g.Clear(Color.White); 
        g.SmoothingMode = SmoothingMode.HighQuality; 
        g.InterpolationMode = InterpolationMode.High; 
        g.DrawImage(image, 0, 0, image.Width, image.Height); 
        if (如果需要填加水印) 
        { 
            switch (水印类型) 
            { 
                //是图片的话                
                case "WM_IMAGE": 
                    this.addWatermarkImage(g, Page.Server.MapPath(Watermarkimgpath), WatermarkPosition, image.Width, image.Height); 
                    break; 
                //如果是文字                     
                case "WM_TEXT": 
                    this.addWatermarkText(g, WatermarkText, WatermarkPosition 
                                               , image.Width, image.Height); 
                    break; 
            } 
            b.Save(newpath); 
            b.Dispose(); 
            image.Dispose(); 
        } 
    } 
    catch 
    { 
        if (File.Exists(oldpath)) 
        { 
            File.Delete(oldpath); 
        } 
    } 
    finally 
    { 
        if (File.Exists(oldpath)) 
        { 
            File.Delete(oldpath); 
        } 
    } 
} 
 
 
加水印文字 /**//// <summary> 
          ///  加水印文字 
          /// </summary> 
          /// <param name="picture">imge 对象</param> 
          /// <param name="_watermarkText">水印文字内容</param> 
          /// <param name="_watermarkPosition">水印位置</param> 
          /// <param name="_width">被加水印图片的宽</param> 
          /// <param name="_height">被加水印图片的高</param> 
          private void addWatermarkText(Graphics picture, string _watermarkText, string _watermarkPosition, int _width, int _height) 
{ 
    int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 }; 
    Font crFont = null; 
    SizeF crSize = new SizeF(); 
    for (int i = 0; i < 7; i++) 
    { 
        crFont = new Font("arial", sizes[i], FontStyle.Bold); 
        crSize = picture.MeasureString(_watermarkText, crFont); 
        if ((ushort)crSize.Width < (ushort)_width) 
            break; 
    } 
    float xpos = 0; 
    float ypos = 0; 
    switch (_watermarkPosition) 
    { 
        case "WM_TOP_LEFT": 
            xpos = ((float)_width * (float).01) + (crSize.Width / 2); 
            ypos = (float)_height * (float).01; 
            break; 
        case "WM_TOP_RIGHT": 
            xpos = ((float)_width * (float).99) - (crSize.Width / 2); 
            ypos = (float)_height * (float).01; 
            break; 
        case "WM_BOTTOM_RIGHT": 
            xpos = ((float)_width * (float).99) - (crSize.Width / 2); 
            ypos = ((float)_height * (float).99) - crSize.Height; 
            break; 
        case "WM_BOTTOM_LEFT": 
            xpos = ((float)_width * (float).01) + (crSize.Width / 2); 
            ypos = ((float)_height * (float).99) - crSize.Height; 
            break; 
    } 
    StringFormat StrFormat = new StringFormat(); 
    StrFormat.Alignment = StringAlignment.Center; 
    SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0)); 
    picture.DrawString(_watermarkText, crFont, semiTransBrush2, xpos + 1, ypos + 1, StrFormat); 
    SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255)); 
    picture.DrawString(_watermarkText, crFont, semiTransBrush, xpos, ypos, StrFormat); 
    semiTransBrush2.Dispose(); 
    semiTransBrush.Dispose(); 
} 
//代码已经修改,可以按比率还填加图片水印,不过如果背景图片和水印图片太不成比率的话(指水印图片要大于背景图片的1/4),出来的效果不是很好。 
水印图片  /**//// <summary> 
          ///  加水印图片 
          /// </summary> 
          /// <param name="picture">imge 对象</param> 
          /// <param name="WaterMarkPicPath">水印图片的地址</param> 
          /// <param name="_watermarkPosition">水印位置</param> 
          /// <param name="_width">被加水印图片的宽</param> 
          /// <param name="_height">被加水印图片的高</param> 
        private void addWatermarkImage(Graphics picture, string WaterMarkPicPath, string _watermarkPosition, int _width, int _height) 
{ 
    Image watermark = new Bitmap(WaterMarkPicPath); 
    ImageAttributes imageAttributes = new ImageAttributes(); 
    ColorMap colorMap = new ColorMap(); 
    colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); 
    colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); 
    ColorMap[] remapTable = { colorMap }; 
    imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); 
    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 colorMatrix = new ColorMatrix(colorMatrixElements); 
    imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); 
    int xpos = 0; 
    int ypos = 0; 
    int WatermarkWidth = 0; 
    int WatermarkHeight = 0; 
    double bl = 1d; 
    //计算水印图片的比率 
    //取背景的1/4宽度来比较 
    if ((_width > watermark.Width * 4) && (_height > watermark.Height * 4)) 
    { 
        bl = 1; 
    } 
    else if ((_width > watermark.Width * 4) && (_height < watermark.Height * 4)) 
    { 
        bl = Convert.ToDouble(_height / 4) / Convert.ToDouble(watermark.Height); 
    } 
    else 
     if ((_width < watermark.Width * 4) && (_height > watermark.Height * 4)) 
    { 
        bl = Convert.ToDouble(_width / 4) / Convert.ToDouble(watermark.Width); 
    } 
    else 
    { 
        if ((_width * watermark.Height) > (_height * watermark.Width)) 
        { 
            bl = Convert.ToDouble(_height / 4) / Convert.ToDouble(watermark.Height); 
        } 
        else 
        { 
            bl = Convert.ToDouble(_width / 4) / Convert.ToDouble(watermark.Width); 
        } 
    } 
    WatermarkWidth = Convert.ToInt32(watermark.Width * bl); 
    WatermarkHeight = Convert.ToInt32(watermark.Height * bl); 
    switch (_watermarkPosition) 
    { 
        case "WM_TOP_LEFT": 
            xpos = 10; 
            ypos = 10; 
            break; 
        case "WM_TOP_RIGHT": 
            xpos = _width - WatermarkWidth - 10; 
            ypos = 10; 
            break; 
        case "WM_BOTTOM_RIGHT": 
            xpos = _width - WatermarkWidth - 10; 
            ypos = _height - WatermarkHeight - 10; 
            break; 
        case "WM_BOTTOM_LEFT": 
            xpos = 10; 
            ypos = _height - WatermarkHeight - 10; 
            break; 
    } 
    picture.DrawImage(watermark, new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes); 
    watermark.Dispose(); 
    imageAttributes.Dispose(); 
} 
 
生成缩略图函数 
        /**//// <summary> 
            /// 生成缩略图 
            /// </summary> 
            /// <param name="oldpath">原图片地址</param> 
            /// <param name="newpath">新图片地址</param> 
            /// <param name="tWidth">缩略图的宽</param> 
            /// <param name="tHeight">缩略图的高</param> 
        private void GreateMiniImage(string oldpath, string newpath, int tWidth, int tHeight) 
{ 
    try 
    { 
        System.Drawing.Image image = System.Drawing.Image.FromFile(oldpath); 
        double bl = 1d; 
        if ((image.Width <= image.Height) && (tWidth >= tHeight)) 
        { 
            bl = Convert.ToDouble(image.Height) / Convert.ToDouble(tHeight); 
        } 
        else if ((image.Width > image.Height) && (tWidth < tHeight)) 
        { 
            bl = Convert.ToDouble(image.Width) / Convert.ToDouble(tWidth); 
        } 
        else 
          if ((image.Width <= image.Height) && (tWidth <= tHeight)) 
        { 
            if (image.Height / tHeight >= image.Width / tWidth) 
            { 
                bl = Convert.ToDouble(image.Width) / Convert.ToDouble(tWidth); 
            } 
            else 
            { 
                bl = Convert.ToDouble(image.Height) / Convert.ToDouble(tHeight); 
            } 
        } 
        else 
        { 
            if (image.Height / tHeight >= image.Width / tWidth) 
            { 
                bl = Convert.ToDouble(image.Height) / Convert.ToDouble(tHeight); 
            } 
            else 
            { 
                bl = Convert.ToDouble(image.Width) / Convert.ToDouble(tWidth); 
            } 
        } 
        Bitmap b = new Bitmap(image, Convert.ToInt32(image.Width / bl), Convert.ToInt32(image.Height / bl)); 
        b.Save(newpath); 
        b.Dispose(); 
        image.Dispose(); 
    } 
    catch { } 
} |