我建议你看看WPF类来完成这项工作(GDI +在Web上下文中不推荐使用)。
的方式(我不知道,如果是最好的方式,但我已经做到了这一点和作品精致漂亮)是类似于:
// Load the original image
BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(physical_imagepath);
image.EndInit();
// Create a final render image
RenderTargetBitmap final = new RenderTargetBitmap(yourNeededWidth, yourNeededHeight, yourDpiDefault, yourDpiDefault, PixelFormats.Default);
DrawingVisual dv = new DrawingVisual();
using (DrawingContext dc = dv.RenderOpen())
{
Rect rectImage = new Rect(0, 0, (double)image.PixelWidth, (double)image.PixelHeight);
dc.DrawImage(image, rectImage);
// Load the bitmap of the watermark
BitmapImage watermark = new BitmapImage();
watermark.BeginInit();
watermark.CacheOption = BitmapCacheOption.OnLoad;
watermark.UriSource = new Uri(physical_logopath);
watermark.EndInit();
// Defines the watermark box
Rect rectWatermark = new Rect(0, 0, (double)watermark.PixelWidth, (double)watermark.PixelHeight);
/* use rectWatermark.X and rectWatermark.Y to move your watermark box around on the final image */
dc.DrawImage(watermark, rectWatermark);
}
final.Render(dv);
// And then serve the final Bitmap to the client
当然所有写成的HttpHandler。 以上代码未经测试。
(小广告:我发布了一个CodeCanyon Item做类似的工作)。
但问题是,它可能降低整个图像的图像质量由于JPEG重而不仅仅是水印所在的块。但我不知道是否有一个简单的方法可以做得更好。 – CodesInChaos 2010-12-02 15:40:18
虽然它会降低图像质量,但只要一次完成就不会成为问题,特别是在平均网页图像的大小/分辨率方面。除非压缩过高,否则最终会产生油漆产生的混乱。 – themaninthesuitcase 2010-12-03 09:55:11