c# 添加图片水印,可以指定水印位置+生成缩略图[付上帅图1,2,3,4]


           早上,一哥儿发来添加图片水印的资料。有三个信息,如下:
            
xx 09:57:35
http://index.cnblogs.com/archive/2004/10/20/54498.aspx 
王二狗 09:57:51
好的,我看看 
  
xx 09:58:12
http://www.iyuanma.com/info/18/17026_200592663244.htm 
xx 10:07:00
http://www.codefans.com/CodeView/CodeView_12043.html 
xx 10:07:18
你看看现成的组件能不能用 
         
         几分钟后,我就发现第二个网址的内容是copy第一个的(也许是相反),真是天下文章一大抄。于是我对那条说有什么组件的东东很感兴趣,下下来一看,吐血,就是一段代码,完全copy第一个文章里的,什么组件啊。真是能欺骗人。
         算了,自己动手,丰衣十足。想起上个月做相册的开发,用到了Gallery开源项目的东西。那里面有填加水印的,并且功能比较强大,能设定位置。不像上面的资料不能调整水印位置,水印效果估计也不好,毕竟就那几行。 其实后来我发现那段代码还是错的,调试通过不了,修改后才能用,至于错在那里在后面介绍
         我们先看看哥儿给我的资料里的代码:
           原来的代码:
ContractedBlock.gif ExpandedBlockStart.gif
 1None.gif private void Btn_Upload_Click(object sender, System.EventArgs e)
 2ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
 3InBlock.gif            if(UploadFile.PostedFile.FileName.Trim()!="")
 4ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 5InBlock.gif                //上传文件
 6InBlock.gif                string extension = Path.GetExtension(UploadFile.PostedFile.FileName).ToUpper();
 7InBlock.gif                string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
 8InBlock.gif                string path = Server.MapPath("."+ "/UploadFile/" + fileName + extension;
 9InBlock.gif                UploadFile.PostedFile.SaveAs(path);
10InBlock.gif
11InBlock.gif                //加文字水印,注意,这里的代码和以下加图片水印的代码不能共存
12InBlock.gif                System.Drawing.Image image = System.Drawing.Image.FromFile(path);
13InBlock.gif                Graphics g = Graphics.FromImage(image);
14InBlock.gif                g.DrawImage(image, 00, image.Width, image.Height);
15InBlock.gif                Font f = new Font("Verdana"32);
16InBlock.gif                Brush b = new SolidBrush(Color.White);
17InBlock.gif                string addText = AddText.Value.Trim();
18InBlock.gif                g.DrawString(addText, f, b, 1010);
19InBlock.gif                g.Dispose();
20InBlock.gif
21InBlock.gif                //加图片水印
22InBlock.gif                System.Drawing.Image image = System.Drawing.Image.FromFile(path);
23InBlock.gif                System.Drawing.Image copyImage = System.Drawing.Image.FromFile( Server.MapPath("."+ "/Alex.gif");
24InBlock.gif                Graphics g = Graphics.FromImage(image);
25InBlock.gif                g.DrawImage(copyImage, new Rectangle(image.Width-copyImage.Width, image.Height-copyImage.Height, copyImage.Width, copyImage.Height), 00, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
26InBlock.gif                g.Dispose();
27InBlock.gif
28InBlock.gif                //保存加水印过后的图片,删除原始图片
29InBlock.gif                string newPath = Server.MapPath("."+ "/UploadFile/" + fileName + "_new" + extension;
30InBlock.gif                image.Save(newPath);
31InBlock.gif                image.Dispose();
32InBlock.gif                if(File.Exists(path))
33ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
34InBlock.gif                    File.Delete(path);
35ExpandedSubBlockEnd.gif                }

36InBlock.gif
37InBlock.gif                Response.Redirect(newPath);
38ExpandedSubBlockEnd.gif            }

39ExpandedBlockEnd.gif        }

         于是我把Gallery里的代码整理了下。如下:

          图片上传函数,进行判断是否加水印,做出两种处理方式:
ContractedBlock.gif ExpandedBlockStart.gif
 1ExpandedBlockStart.gifContractedBlock.gif    /**//// <summary>
 2InBlock.gif        /// 上传图片代码
 3InBlock.gif        /// </summary>
 4InBlock.gif        /// <param name="image_file">HtmlInputFile控件</param>
 5InBlock.gif        /// <param name="ImgPath">存放的文件夹绝对位置</param>
 6InBlock.gif        /// <param name="ImgLink">生成的图片的名称带后缀</param>
 7ExpandedBlockEnd.gif        /// <returns></returns>

 8None.gif        public bool UpPic(System.Web.UI.HtmlControls.HtmlInputFile image_file,string ImgPath,string ImgLink )
 9ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
10InBlock.gif            if(image_file.PostedFile.FileName!=null && image_file.PostedFile.FileName.Trim()!="")
11ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
12InBlock.gif                try
13ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
14InBlock.gif                    if!System.IO.Directory.Exists(ImgPath))
15ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
16InBlock.gif                        System.IO.Directory.CreateDirectory( ImgPath);
17ExpandedSubBlockEnd.gif                    }

18InBlock.gif                     //生成缩略图
                            this.GreateMiniImage((ImgPath+ "\\"+"old_"+ImgLink),(ImgPath+ "\\"+"mini_"+ImgLink),50,50);
19InBlock.gif                    //如果显示水印
20InBlock.gif                    if(ShowWatermark)
21ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
22InBlock.gif                        image_file.PostedFile.SaveAs(ImgPath+ "\\" +"old_"+ImgLink);
23InBlock.gif                        //加水印
24InBlock.gif                        this.addWaterMark((ImgPath+ "\\"+"old_"+ImgLink),(ImgPath+ "\\"+ImgLink));
25InBlock.gif
26InBlock.gif
27InBlock.gif                    
28ExpandedSubBlockEnd.gif                    }

29InBlock.gif                    else
30ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
31InBlock.gif                        image_file.PostedFile.SaveAs(ImgPath+ "\\" +ImgLink);
32InBlock.gif                    
33InBlock.gif                    
34ExpandedSubBlockEnd.gif                    }

35InBlock.gif                    return true;
36ExpandedSubBlockEnd.gif                }

37InBlock.gif
38InBlock.gif                catch
39ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
40InBlock.gif                    return false;
41ExpandedSubBlockEnd.gif                }

42ExpandedSubBlockEnd.gif            }

43InBlock.gif            else
44InBlock.gif            
45ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
46InBlock.gif                return false;
47ExpandedSubBlockEnd.gif            }

48InBlock.gif
49ExpandedBlockEnd.gif        }

   加水印的函数如下:
   填加图片函数,需要下面两个函数的支持,当然也可以写到一起,不过那看起来就很冗长了。
ContractedBlock.gif ExpandedBlockStart.gif 添加图片水印
 1ExpandedBlockStart.gifContractedBlock.gif    /**//// <summary>
 2InBlock.gif            /// 添加图片水印
 3InBlock.gif            /// </summary>
 4InBlock.gif            /// <param name="oldpath">原图片绝对地址</param>
 5ExpandedBlockEnd.gif            /// <param name="newpath">新图片放置的绝对地址</param>

 6None.gif        private void addWaterMark(string oldpath,string newpath)
 7None.gif        
 8ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
 9InBlock.gif            try
10ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
11InBlock.gif
12InBlock.gif                System.Drawing.Image image = System.Drawing.Image.FromFile(oldpath);
13InBlock.gif
14InBlock.gif                Bitmap b = new Bitmap(image.Width, image.Height,PixelFormat.Format24bppRgb);
15InBlock.gif                Graphics g = Graphics.FromImage(b);
16InBlock.gif                g.Clear(Color.White);
17InBlock.gif                g.SmoothingMode = SmoothingMode.HighQuality;
18InBlock.gif                g.InterpolationMode = InterpolationMode.High;
19InBlock.gif            
20InBlock.gif                g.DrawImage(image, 00, image.Width, image.Height);
21InBlock.gif
22InBlock.gif                if(如果需要填加水印)
23ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
24InBlock.gif                    switch(水印类型)
25ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
26InBlock.gif    //是图片的话               
                                   case "WM_IMAGE":
27InBlock.gif                            this.addWatermarkImage( g,Page.Server.MapPath(Watermarkimgpath),WatermarkPosition,image.Width,image.Height);
28InBlock.gif                            break;
29InBlock.gif    //如果是文字                    
                                   case "WM_TEXT":
30InBlock.gif                            this.addWatermarkText( g, WatermarkText,WatermarkPosition
31InBlock.gif                                ,image.Width,image.Height);
32InBlock.gif                            break;
33ExpandedSubBlockEnd.gif                    }
34InBlock.gif
35InBlock.gif                    b.Save(newpath);
36InBlock.gif                    b.Dispose();
37InBlock.gif                    image.Dispose();
38ExpandedSubBlockEnd.gif                }

39InBlock.gif
40ExpandedSubBlockEnd.gif            }

41InBlock.gif            catch
42ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
43InBlock.gif            
44InBlock.gif                if(File.Exists(oldpath))
45ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
46InBlock.gif                    File.Delete(oldpath);
47ExpandedSubBlockEnd.gif                }

48ExpandedSubBlockEnd.gif            }

49InBlock.gif            finally
50ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
51InBlock.gif                
52InBlock.gif                if(File.Exists(oldpath))
53ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
54InBlock.gif                    File.Delete(oldpath);
55ExpandedSubBlockEnd.gif                }

56InBlock.gif            
57InBlock.gif            
58ExpandedSubBlockEnd.gif            }

59InBlock.gif        
60InBlock.gif        
61InBlock.gif
62InBlock.gif
63InBlock.gif
64InBlock.gif        
65ExpandedBlockEnd.gif        }

  
ContractedBlock.gif ExpandedBlockStart.gif 加水印文字
 1ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
 2InBlock.gif        ///  加水印文字
 3InBlock.gif        /// </summary>
 4InBlock.gif        /// <param name="picture">imge 对象</param>
 5InBlock.gif        /// <param name="_watermarkText">水印文字内容</param>
 6InBlock.gif        /// <param name="_watermarkPosition">水印位置</param>
 7InBlock.gif        /// <param name="_width">被加水印图片的宽</param>
 8ExpandedBlockEnd.gif        /// <param name="_height">被加水印图片的高</param>

 9None.gif        private void addWatermarkText( Graphics picture,string _watermarkText,string _watermarkPosition,int _width,int _height)
10ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
11ExpandedSubBlockStart.gifContractedSubBlock.gif            int[] sizes = new int[]dot.gif{16,14,12,10,8,6,4};
12InBlock.gif            Font crFont = null;
13InBlock.gif            SizeF crSize = new    SizeF();
14InBlock.gif            for (int i=0 ;i<7; i++)
15ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
16InBlock.gif                crFont = new Font("arial", sizes[i], FontStyle.Bold);
17InBlock.gif                crSize = picture.MeasureString(_watermarkText, crFont);
18InBlock.gif
19InBlock.gif                if((ushort)crSize.Width < (ushort)_width)
20InBlock.gif                    break;
21ExpandedSubBlockEnd.gif            }

22InBlock.gif
23InBlock.gif            float xpos = 0;
24InBlock.gif            float ypos = 0;
25InBlock.gif
26InBlock.gif            switch(_watermarkPosition)
27ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
28InBlock.gif                case "WM_TOP_LEFT":
29InBlock.gif                    xpos = ((float)_width * (float).01+ (crSize.Width / 2);
30InBlock.gif                    ypos = (float)_height * (float).01;
31InBlock.gif                    break;
32InBlock.gif                case "WM_TOP_RIGHT":
33InBlock.gif                    xpos = ((float)_width * (float).99- (crSize.Width / 2);
34InBlock.gif                    ypos = (float)_height * (float).01;
35InBlock.gif                    break;
36InBlock.gif                case "WM_BOTTOM_RIGHT":
37InBlock.gif                    xpos = ((float)_width * (float).99- (crSize.Width / 2);
38InBlock.gif                    ypos = ((float)_height * (float).99- crSize.Height;
39InBlock.gif                    break;
40InBlock.gif                case "WM_BOTTOM_LEFT":
41InBlock.gif                    xpos = ((float)_width * (float).01+ (crSize.Width / 2);
42InBlock.gif                    ypos = ((float)_height * (float).99- crSize.Height;
43InBlock.gif                    break;
44ExpandedSubBlockEnd.gif            }

45InBlock.gif
46InBlock.gif            StringFormat StrFormat = new StringFormat();
47InBlock.gif            StrFormat.Alignment = StringAlignment.Center;
48InBlock.gif
49InBlock.gif            SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(15300,0));
50InBlock.gif            picture.DrawString(_watermarkText, crFont, semiTransBrush2, xpos+1, ypos+1, StrFormat);
51InBlock.gif
52InBlock.gif            SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153255255255));
53InBlock.gif            picture.DrawString(_watermarkText, crFont, semiTransBrush, xpos, ypos, StrFormat);
54InBlock.gif
55InBlock.gif
56InBlock.gif            semiTransBrush2.Dispose();
57InBlock.gif            semiTransBrush.Dispose();
58InBlock.gif
59InBlock.gif
60InBlock.gif
61ExpandedBlockEnd.gif        }
//代码已经修改,可以按比率还填加图片水印,不过如果背景图片和水印图片太不成比率的话(指水印图片要大于背景图片的1/4),出来的效果不是很好。
ContractedBlock.gif ExpandedBlockStart.gif 水印图片
  1ExpandedBlockStart.gifContractedBlock.gif  /**//// <summary>
  2InBlock.gif        ///  加水印图片
  3InBlock.gif        /// </summary>
  4InBlock.gif        /// <param name="picture">imge 对象</param>
  5InBlock.gif        /// <param name="WaterMarkPicPath">水印图片的地址</param>
  6InBlock.gif        /// <param name="_watermarkPosition">水印位置</param>
  7InBlock.gif        /// <param name="_width">被加水印图片的宽</param>
  8ExpandedBlockEnd.gif        /// <param name="_height">被加水印图片的高</param>

  9None.gif        private void addWatermarkImage( Graphics picture,string WaterMarkPicPath,string _watermarkPosition,int _width,int _height)
 10ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
 11InBlock.gif            Image watermark = new Bitmap(WaterMarkPicPath);
 12InBlock.gif
 13InBlock.gif            ImageAttributes imageAttributes = new ImageAttributes();
 14InBlock.gif            ColorMap colorMap = new ColorMap();
 15InBlock.gif
 16InBlock.gif            colorMap.OldColor = Color.FromArgb(25502550);
 17InBlock.gif            colorMap.NewColor = Color.FromArgb(0000);
 18ExpandedSubBlockStart.gifContractedSubBlock.gif            ColorMap[] remapTable = dot.gif{colorMap};
 19InBlock.gif
 20InBlock.gif            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
 21InBlock.gif
 22ExpandedSubBlockStart.gifContractedSubBlock.gif            float[][] colorMatrixElements = dot.gif{
 23ExpandedSubBlockStart.gifContractedSubBlock.gif                                                new float[] dot.gif{1.0f,  0.0f,  0.0f,  0.0f0.0f},
 24ExpandedSubBlockStart.gifContractedSubBlock.gif                                                new float[] dot.gif{0.0f,  1.0f,  0.0f,  0.0f0.0f},
 25ExpandedSubBlockStart.gifContractedSubBlock.gif                                                new float[] dot.gif{0.0f,  0.0f,  1.0f,  0.0f0.0f},
 26ExpandedSubBlockStart.gifContractedSubBlock.gif                                                new float[] dot.gif{0.0f,  0.0f,  0.0f,  0.3f0.0f},
 27ExpandedSubBlockStart.gifContractedSubBlock.gif                                                new float[] dot.gif{0.0f,  0.0f,  0.0f,  0.0f1.0f}
 28ExpandedSubBlockEnd.gif                                            }
;
 29InBlock.gif
 30InBlock.gif            ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
 31InBlock.gif
 32InBlock.gif            imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
 33InBlock.gif
 34InBlock.gif            int xpos = 0;
 35InBlock.gif            int ypos = 0;
 36InBlock.gif            int WatermarkWidth=0;
 37InBlock.gif            int WatermarkHeight=0;
 38InBlock.gif            double bl=1d;
 39InBlock.gif            //计算水印图片的比率
 40InBlock.gif            //取背景的1/4宽度来比较
 41InBlock.gif            if((_width>watermark.Width*4)&&(_height>watermark.Height*4))
 42ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 43InBlock.gif                bl=1;
 44ExpandedSubBlockEnd.gif            }

 45InBlock.gif            else if((_width>watermark.Width*4)&&(_height<watermark.Height*4))
 46ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 47InBlock.gif                bl=Convert.ToDouble(_height/4)/Convert.ToDouble(watermark.Height);
 48InBlock.gif            
 49ExpandedSubBlockEnd.gif            }
else
 50InBlock.gif            
 51InBlock.gif            if((_width<watermark.Width*4)&&(_height>watermark.Height*4))
 52ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 53InBlock.gif                bl=Convert.ToDouble(_width/4)/Convert.ToDouble(watermark.Width);
 54ExpandedSubBlockEnd.gif            }

 55InBlock.gif            else
 56ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 57InBlock.gif                if((_width*watermark.Height)>(_height*watermark.Width))
 58ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 59InBlock.gif                    bl=Convert.ToDouble(_height/4)/Convert.ToDouble(watermark.Height);
 60InBlock.gif                    
 61ExpandedSubBlockEnd.gif                }

 62InBlock.gif                else
 63ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 64InBlock.gif                    bl=Convert.ToDouble(_width/4)/Convert.ToDouble(watermark.Width);
 65InBlock.gif                    
 66ExpandedSubBlockEnd.gif                }

 67InBlock.gif            
 68ExpandedSubBlockEnd.gif            }

 69InBlock.gif
 70InBlock.gif            WatermarkWidth=Convert.ToInt32(watermark.Width*bl);
 71InBlock.gif            WatermarkHeight=Convert.ToInt32(watermark.Height*bl);
 72InBlock.gif
 73InBlock.gif            
 74InBlock.gif
 75InBlock.gif            switch(_watermarkPosition)
 76ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 77InBlock.gif                case "WM_TOP_LEFT":
 78InBlock.gif                    xpos = 10;
 79InBlock.gif                    ypos = 10;
 80InBlock.gif                    break;
 81InBlock.gif                case "WM_TOP_RIGHT":
 82InBlock.gif                    xpos = _width - WatermarkWidth - 10;
 83InBlock.gif                    ypos = 10;
 84InBlock.gif                    break;
 85InBlock.gif                case "WM_BOTTOM_RIGHT":
 86InBlock.gif                    xpos = _width - WatermarkWidth - 10;
 87InBlock.gif                    ypos = _height -WatermarkHeight - 10;
 88InBlock.gif                    break;
 89InBlock.gif                case "WM_BOTTOM_LEFT":
 90InBlock.gif                    xpos = 10;
 91InBlock.gif                    ypos = _height - WatermarkHeight - 10;
 92InBlock.gif                    break;
 93ExpandedSubBlockEnd.gif            }

 94InBlock.gif
 95InBlock.gif            picture.DrawImage(watermark, new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight), 00, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
 96InBlock.gif
 97InBlock.gif
 98InBlock.gif            watermark.Dispose();
 99InBlock.gif            imageAttributes.Dispose();
100ExpandedBlockEnd.gif        }

   生成缩略图函数
ContractedBlock.gif ExpandedBlockStart.gif 生成缩略图
 1None.gif
 2ExpandedBlockStart.gifContractedBlock.gif        /**//// <summary>
 3InBlock.gif        /// 生成缩略图
 4InBlock.gif        /// </summary>
 5InBlock.gif        /// <param name="oldpath">原图片地址</param>
 6InBlock.gif        /// <param name="newpath">新图片地址</param>
 7InBlock.gif        /// <param name="tWidth">缩略图的宽</param>
 8ExpandedBlockEnd.gif        /// <param name="tHeight">缩略图的高</param>

 9None.gif        private void  GreateMiniImage(string oldpath,string newpath,int tWidth, int tHeight)
10ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
11InBlock.gif        
12InBlock.gif            try
13ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
14InBlock.gif
15InBlock.gif                System.Drawing.Image image = System.Drawing.Image.FromFile(oldpath);
16InBlock.gif                double bl=1d;
17InBlock.gif                if((image.Width<=image.Height)&&(tWidth>=tHeight))
18ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
19InBlock.gif                    bl=Convert.ToDouble(image.Height)/Convert.ToDouble(tHeight);
20ExpandedSubBlockEnd.gif                }

21InBlock.gif                else if((image.Width>image.Height)&&(tWidth<tHeight))
22ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
23InBlock.gif                    bl=Convert.ToDouble(image.Width)/Convert.ToDouble(tWidth);
24InBlock.gif            
25ExpandedSubBlockEnd.gif                }

26InBlock.gif                else
27InBlock.gif            
28InBlock.gif                    if((image.Width<=image.Height)&&(tWidth<=tHeight))
29ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
30InBlock.gif                    if(image.Height/tHeight>=image.Width/tWidth)
31ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
32InBlock.gif                        bl=Convert.ToDouble(image.Width)/Convert.ToDouble(tWidth);
33InBlock.gif                    
34ExpandedSubBlockEnd.gif                    }

35InBlock.gif                    else
36ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
37InBlock.gif                        bl=Convert.ToDouble(image.Height)/Convert.ToDouble(tHeight);
38ExpandedSubBlockEnd.gif                    }

39ExpandedSubBlockEnd.gif                }

40InBlock.gif                else
41ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
42InBlock.gif                    if(image.Height/tHeight>=image.Width/tWidth)
43ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
44InBlock.gif                        bl=Convert.ToDouble(image.Height)/Convert.ToDouble(tHeight);
45InBlock.gif                    
46ExpandedSubBlockEnd.gif                    }

47InBlock.gif                    else
48ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
49InBlock.gif                        bl=Convert.ToDouble(image.Width)/Convert.ToDouble(tWidth);
50InBlock.gif                    
51ExpandedSubBlockEnd.gif                    }

52InBlock.gif            
53ExpandedSubBlockEnd.gif                }

54InBlock.gif
55InBlock.gif            
56InBlock.gif                Bitmap b = new Bitmap(image ,Convert.ToInt32(image.Width/bl), Convert.ToInt32(image.Height/bl));
57InBlock.gif
58InBlock.gif                b.Save(newpath);
59InBlock.gif                b.Dispose();
60InBlock.gif                image.Dispose();
61InBlock.gif                
62InBlock.gif
63ExpandedSubBlockEnd.gif            }

64InBlock.gif            catch
65ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
66InBlock.gif            
67InBlock.gif                
68ExpandedSubBlockEnd.gif            }

69InBlock.gif            
70ExpandedBlockEnd.gif        }

     如果你能耐着心读到这里,你可以分辨一下,这两个加水印的函数和网上别人的代码有什么不同了。你也可以发现为什么网上的代码不能运行通过了。你只要动下小手,调试下就知道原因了。

     最后做得效果很好,附上帅图1,2,3
  1632846984867812500.gif带图片水印的。 17632846983036875000.gif带文字水印

   你看看效果不错吧,这些水印都是设为放在右下角的。 至于带图片的那张怎么位置不像在右下角,是因为背景图片太小,水印图片太大的原因。 我只是随便做了下测试。新的效果图已经放上。
      如果你也是像我这样菜鸟的话,可能对你有点用处。大侠就不用看了。我写出来,其实是觉得网上连一些基础的代码都写的不好,还抄来抄去,更严重的是还是错误的。 12.gif
     最新帅图: 1632848402105625000.JPG
     缩略图:
     mini.JPG由于原图太大上传不上来,只得把对比图发上来。

   版权所有:wangergo.cnblogs.com ,王传炜,2005-5-31

转载于:https://www.cnblogs.com/wangergo/archive/2006/05/31/414239.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值