水印图像不起作用,获取空白图像

时间:2013-12-14 14:21:46

标签: c# asp.net watermark

我正在尝试在ASP.NET中的图像上应用水印文本。我想使用Image流而不是使用/写入本地文件。

以下是代码:

 using (MemoryStream ms_small = new MemoryStream())
  {

       System.Drawing.Image image_small = System.Drawing.Image.FromStream(filestream); 
       width = image_small.Width;
       height = image_small.Height;


       string Copyright = "Hi I am copyright watermark";

       Graphics grPhoto = Graphics.FromImage(image_small);

       int phWidth = image_small.Width;
       int phHeight = image_small.Height;

        grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

       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 = grPhoto.MeasureString(Copyright, crFont);
         if ((ushort)crSize.Width < (ushort)phWidth)
            break;
       }

       int yPixlesFromBottom = (int)(phHeight * .05);

       float yPosFromBottom = ((phHeight - yPixlesFromBottom) - (crSize.Height / 2));

       float xCenterOfImg = (phWidth / 2);


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


       SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));


        grPhoto.DrawString(Copyright,             
               crFont,                                  
               semiTransBrush2,                         
               new PointF(xCenterOfImg + 1, yPosFromBottom + 1),
                            StrFormat);

            SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
            grPhoto.DrawString(Copyright,            
                 crFont,                           
                 semiTransBrush,                          
                 new PointF(xCenterOfImg, yPosFromBottom), 
                 StrFormat);                      

                   Bitmap outputBitMap = new Bitmap(image_small.Width, image_small.Height, grPhoto);
                   image_small = (Image)outputBitMap;
                   ImageBuilder.Current.Build(image_small, ms_small, rs_small_jpg);

               }

水印代码来自here

同样,我想要实现的是图像顶部的水印文本,而无需在本地写入图像或使用临时本地图像文件来应用水印。我得到的是空白图像,而不是带有水印的图像。

2 个答案:

答案 0 :(得分:1)

您的问题是在方法结束时用于图像的图形。相反,你必须使用原始图像,因为所有图形都在其中绘制。

替换此字符串:

Bitmap outputBitMap = new Bitmap(image_small.Width, image_small.Height, grPhoto);

有了这个:

Bitmap outputBitMap = new Bitmap(imageSmall);
祝你好运!

答案 1 :(得分:0)

您的问题不明确,如果您需要在asp.net HttpHandler中向页面显示生成的图像是您想要的。 This帖子就是样本 希望这会有所帮助。