2011-03-28 93 views
4

我所看到的关于adding watermark on images with phpASP.NET:添加“水印”到图像上飞

我愿做同样的重大问题和答案,这一次与ASP.NET

因此,这里有几个问题。

  1. 我该如何做到这一点与ASP?
  2. 这个过程是否会对服务器造成巨大的负担?
  3. 我可以使用图像的水印,而不是简单的文字?
+0

[我的开源imageresizing.net(http://imageresizing.net)项目允许您定义多个图片和文字层(或他们的群体)作为水印使用。完全XML配置,请参阅[文档页面](http://imageresizing.net/plugins/watermark)。您可以通过将图片添加到图片网址来引用水印,如image.jpg?watermark = logo。您可以使用“Pipeline.Rewrite”事件处理程序轻松实施水印;您想要应用的任何规则都可用。而且,它兼容ASP.NET和ASP,因为它具有URL API。 – 2011-12-09 07:43:03

回答

3

下面是来自codeproject的另一个示例http://www.codeproject.com/KB/web-image/ASPImaging1.aspx,您可以对图像执行多种操作,包括从图像添加水印。

我觉得这个过程就是取得cpu的功率ether是在php上,以太网在asp.net上。所以这种作品必须有一个图像缓存模式。

这里是一些基本的代码。在这段代码中,你必须改变水印的位置和图像的大小。水印可以是透明的png图像。

public void MakePhoto(...parametres...) 
    { 
     Bitmap outputImage = null; 
     Graphics g = null; 

     try 
     {     
      // the final image 
      outputImage = new Bitmap(OutWidth, OutHeight, PixelFormat.Format24bppRgb); 

      g = Graphics.FromImage(outputImage); 
      g.CompositingMode = CompositingMode.SourceCopy; 
      Rectangle destRect = new Rectangle(0, 0, OutWidth, OutHeight); 

      // the photo 
      using (var BasicPhoto = new Bitmap(cBasicPhotoFileOnDisk)) 
      { 
       g.DrawImage(BasicPhoto, destRect, 0, 0, BasicPhoto.Width, BasicPhoto.Height, GraphicsUnit.Pixel); 
      } 

      g.CompositingMode = CompositingMode.SourceOver; 
      // the watermark 
      using (var WaterMark = new Bitmap(cWaterMarkPhotoOnDisk)) 
      { 
       Rectangle destWaterRect = new Rectangle(0, 0, OutWidth, OutHeight); 

       g.DrawImage(WaterMark, destWaterRect, 0, 0, OutWidth, OutHeight, GraphicsUnit.Pixel); 
      } 

      outputImage.Save(TheFileNameTosaveIt, ImageFormat.Jpeg); 

     } 
     catch (Exception x) 
     { 
      Debug.Assert(false); 
      ... log your error, and send an error image....     
     } 
     finally 
     { 
      if (outputImage != null) 
       outputImage.Dispose(); 

      if (g != null) 
       g.Dispose(); 
     } 
    } 

如果您希望自定义句柄,上面的代码是站立的,但您只更改保存行。就像是。

public void ProcessRequest (HttpContext context)  
{ 
    context.Response.ContentType = "image/jpeg"; 

    // add you cache here 
    context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(200)); 
    context.Response.Cache.SetMaxAge(new TimeSpan(0, 200, 0)); 
    context.Response.BufferOutput = false; 


    ..... the above code.... 
    outputImage.Save(context.Response.OutputStream, ImageFormat.Jpeg); 
    ..... the above code.... 


    context.Response.End(); 
} 
1

是的,你可以通过使用GDI+,在图像使用DrawString(),然后将其保存或恢复它作为响应做到这一点。

1

post I made中,有一个使用WPF而不是旧的,弃用的GDI +为图像上的文本加水印的示例。

正如你在文章中看到的那样,文本是通过使用DrawingContext的DrawText方法添加的,真的很容易使用DrawImage来代替,它接受一个BitmapImage。

的东西,如:

BitmapImage logo = new BitmapImage(); 
logo.BeginInit(); 
logo.CacheOption = BitmapCacheOption.OnLoad; 
logo.UriSource = new Uri(your_physical_logopath); 
logo.EndInit(); 

Rect rect = new Rect(0, 0, (double)logo.PixelWidth, (double)logo.PixelHeight); 

dc.DrawImage(logo, rect); 

随着rect.X和rect.Y,你执行的DrawImage(前),你可以修改的DrawingContext内的标志图像的相对位置。

+0

您提供的链接已损坏。 – 2015-11-02 11:34:59