winforms – C# – 通过特殊的方式将水印添加到照片中

winforms – C# – 通过特殊的方式将水印添加到照片中,第1张

概述我需要通过特殊的方式为照片添加水印.我知道如何做,但我不知道如何做到与第 http://www.photoshopessentials.com/photo-effects/copyright/条相同的方式 这里是添加水印的方法.我可以如何改变它以获得水印图像,如上面的文章? public static Bitmap AddWatermark(this Bitmap originalImage, B 我需要通过特殊的方式为照片添加水印.我知道如何做,但我不知道如何做到与第 http://www.photoshopessentials.com/photo-effects/copyright/条相同的方式

这里是添加水印的方法.我可以如何改变它以获得水印图像,如上面的文章?

public static Bitmap AdDWatermark(this Bitmap originalimage,Bitmap watermarkImage,WatermarkLocationEnum location){    int offsetWIDth;    int offsetHeight;    if ((watermarkImage.WIDth > originalimage.WIDth) | (watermarkImage.Height > originalimage.Height))        throw new Exception("The watermark must be smaller than the original image.");    Bitmap backgroundImage = new Bitmap((Bitmap)originalimage.Clone());    Bitmap image = new Bitmap(backgroundImage.WIDth,backgroundImage.Height);    Graphics graphics = Graphics.FromImage(image);    offsetWIDth = GetoffsetWIDth(image.WIDth,watermarkImage.WIDth,location);    offsetHeight = GetoffsetHeight(image.Height,watermarkImage.Height,location);    watermarkImage.SetResolution(backgroundImage.HorizontalResolution,backgroundImage.VerticalResolution);    offsetWIDth = Math.Max(offsetWIDth - 1,0);    offsetHeight = Math.Max(offsetHeight - 1,0);    graphics.DrawImage(watermarkImage,offsetWIDth,offsetHeight);    for (int i = offsetWIDth; i < (offsetWIDth + watermarkImage.WIDth); i++)    {        for (int j = offsetHeight; j < (offsetHeight + watermarkImage.Height); j++)        {            color pixel = image.GetPixel(i,j);            if (pixel.A > 0)            {                color color = color.FromArgb(pixel.A,pixel.R,pixel.G,pixel.B);                color imagePixelcolor = backgroundImage.GetPixel(i,j);                double Alpha = (double)color.A / 255;                color newcolor = color.FromArgb(255,(int)((double)imagePixelcolor.R * (1.0 - Alpha) + Alpha * color.R),(int)((double)imagePixelcolor.G * (1.0 - Alpha) + Alpha * color.G),(int)((double)imagePixelcolor.B * (1.0 - Alpha) + Alpha * color.B));                backgroundImage.SetPixel(i,j,newcolor);            }        }    }    return backgroundImage;}//............Image img = Bitmap.Fromfile("DSC00766.JPG");var wtm = (Bitmap)Bitmap.Fromfile("copyright1.jpg");((Bitmap)img).AdDWatermark(wtm,WatermarkLocationEnum.BottomCenter).Save("new.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);

UPDATE

1附加结果
2附件结果

解决方法 如果您创建您的版权图像,使其是半透明的并且具有这样的透明背景(使用Paint.NET):

您可以从中创建一个TextureBrush,并使用它来绘制原始图像的版权:

private voID button2_Click(object sender,EventArgs e){    using (Image image = Image.Fromfile(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"))    using (Image watermarkImage = Image.Fromfile(@"C:\Users\Public\Pictures\Sample Pictures\watermark.png"))        using (Graphics imageGraphics = Graphics.FromImage(image))    using (Brush watermarkBrush = new TextureBrush(watermarkImage))    {        imageGraphics.FillRectangle(watermarkBrush,new Rectangle(new Point(0,0),image.Size));        image.Save(@"C:\Users\Public\Pictures\Sample Pictures\Desert_watermark.jpg");    }}

这产生了这个结果:

总结

以上是内存溢出为你收集整理的winforms – C# – 通过特殊的方式将水印添加到照片中全部内容,希望文章能够帮你解决winforms – C# – 通过特殊的方式将水印添加到照片中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1239539.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存