【问题标题】:Watermark image doesn't work, get Blank image水印图像不起作用,获取空白图像
【发布时间】:2013-12-14 23:09:32
【问题描述】:

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

代码如下:

 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

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

【问题讨论】:

标签: c# asp.net watermark


【解决方案1】:

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

替换这个字符串:

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

有了这个:

Bitmap outputBitMap = new Bitmap(imageSmall);

祝你好运!

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多