C# 水印图像不';不工作,得到空白图像

C# 水印图像不';不工作,得到空白图像,c#,asp.net,watermark,C#,Asp.net,Watermark,我正在尝试在ASP.NET中的图像上应用水印文本。我想使用图像流,而不是使用/写入本地文件 代码如下: using (MemoryStream ms_small = new MemoryStream()) { System.Drawing.Image image_small = System.Drawing.Image.FromStream(filestream); width = image_small.Width; height = ima

我正在尝试在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);

               }
使用(MemoryStream ms_small=new MemoryStream())
{
System.Drawing.Image\u small=System.Drawing.Image.FromStream(filestream);
宽度=图像\小宽度;
高度=图像\小高度;
string Copyright=“嗨,我是版权水印”;
Graphics grPhoto=Graphics.FromImage(图像小);
int phWidth=image_small.Width;
int phHeight=图像\小高度;
grPhoto.SmoothingMode=SmoothingMode.AntiAlias;
int[]大小=新的int[]{16,14,12,10,8,6,4};
Font crFont=null;
SizeF crSize=新的SizeF();
对于(int i=0;i<7;i++)
{
crFont=新字体(“arial”,大小[i],字体样式.粗体);
crSize=grPhoto.MeasureString(版权所有,crFont);
如果((ushort)crSize.Width<(ushort)phWidth)
打破
}
int-yPixlesFromBottom=(int)(phHeight*.05);
浮动yPosFromBottom=((phHeight-yPixlesFromBottom)-(crSize.Height/2));
浮点数=FIMG=(phWidth/2);
StringFormat StrFormat=新StringFormat();
StrFormat.Alignment=StringAlignment.Center;
SolidBrush-semiTransBrush2=新的SolidBrush(颜色为argb(153,0,0,0));
grPhoto.DrawString(版权所有,
crFont,
半反式,
新的点F(xCenterOfImg+1,yPosFromBottom+1),
标准格式);
SolidBrush semiTransBrush=新的SolidBrush(Color.FromArgb(153255 255 255));
grPhoto.DrawString(版权所有,
crFont,
半Transbrush,
新的点F(xCenterOfImg、yPosFromBottom),
标准格式);
位图输出位图=新位图(image\u small.Width、image\u small.Height、grPhoto);
图像_小=(图像)输出位图;
ImageBuilder.Current.Build(image\u small、ms\u small、rs\u small\u jpg);
}
水印代码来自


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

您的问题不清楚,如果您需要在asp.net
HttpHandler中向页面显示生成的图像,这是您想要的。
波斯特就是其中的一个例子

希望这有帮助。

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

替换此字符串:

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

Bitmap outputBitMap = new Bitmap(imageSmall);

祝你好运

生成图像后,您希望如何处理它?你的代码是在HttpHandler里面还是什么?我已经编辑了你的标题。请参阅“”,其中的共识是“不,他们不应该”。