将透明水印 (png)图像添加到另一个图像

本文关键字:图像 添加 另一个 png 透明 | 更新日期: 2023-09-27 18:33:44

我需要为较大的图像添加透明水印。到目前为止,我来了所有的谷歌搜索代码似乎有效,但水图像放置不正确,似乎失真并失去透明度。请让我知道如何纠正此问题。如果有更好的方法来实现这一目标,请告诉我。

主图片 640x480

水印图像 100x86

public static class ImageHelper
{
    #region Public Methods and Operators
    public static Bitmap BitmapSourceToBitmap(BitmapSource bitmapsource)
    {
        Bitmap bitmap;
        using (var outStream = new MemoryStream())
        {
            BitmapEncoder enc = new BmpBitmapEncoder();
            enc.Frames.Add(BitmapFrame.Create(bitmapsource));
            enc.Save(outStream);
            bitmap = new Bitmap(outStream);
        }
        return bitmap;
    }
    public static BitmapSource BitmapToBitmapSource(Bitmap source)
    {
        return Imaging.CreateBitmapSourceFromHBitmap(
            source.GetHbitmap(), 
            IntPtr.Zero, 
            Int32Rect.Empty, 
            BitmapSizeOptions.FromEmptyOptions());
    }
    #endregion
    #region Methods
    public static Image BitmapSourceToImage(BitmapSource image)
    {
        var ms = new MemoryStream();
        var encoder = new BmpBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(image));
        encoder.Save(ms);
        ms.Flush();
        return Image.FromStream(ms);
    }
    public static Bitmap Superimpose(Bitmap largeBmp, Bitmap smallBmp)
    {
        Graphics g = Graphics.FromImage(largeBmp);
        g.CompositingMode = CompositingMode.SourceOver;
        // smallBmp.MakeTransparent();
        int margin = 5;
        int x = largeBmp.Width - smallBmp.Width - margin;
        int y = largeBmp.Height - smallBmp.Height - margin;
        g.DrawImage(smallBmp, new Point(x, y));
        return largeBmp;
    }
    #endregion
}

调用代码。

        var fs = new FileStream(path, FileMode.Create);
        BitmapSource bitmap = new BitmapImage(new Uri(ImagePath, UriKind.Absolute));
        BitmapSource Logobitmap = new BitmapImage(new Uri(logoPath, UriKind.Absolute));
        bitmap =
            ImageHelper.BitmapToBitmapSource(
                ImageHelper.Superimpose(
                    ImageHelper.BitmapSourceToBitmap(bitmap), 
                    ImageHelper.BitmapSourceToBitmap(Logobitmap)));
        var encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bitmap));
        encoder.Save(fs);
        fs.Close();

将透明水印 (png)图像添加到另一个图像

任何需要它的人修复了它 现成的:)

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using Point = System.Drawing.Point;
public static class ImageHelper
{
    #region Enums
    public enum ImageType
    {
        Bitmap = 0, 
        PNG = 1
    }
    #endregion
    #region Public Methods and Operators
    public static Bitmap BitmapSourceToBitmap(BitmapSource bitmapsource, ImageType type = ImageType.Bitmap)
    {
        Bitmap bitmap;
        using (var outStream = new MemoryStream())
        {
            BitmapEncoder enc = null;
            if (type == ImageType.Bitmap)
            {
                enc = new BmpBitmapEncoder();
            }
            else
            {
                enc = new PngBitmapEncoder();
            }
            enc.Frames.Add(BitmapFrame.Create(bitmapsource));
            enc.Save(outStream);
            bitmap = new Bitmap(outStream);

        }
        return bitmap;
    }
    public static Image BitmapSourceToImage(BitmapSource image)
    {
        var ms = new MemoryStream();
        var encoder = new BmpBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(image));
        encoder.Save(ms);
        ms.Flush();
        return Image.FromStream(ms);
    }
    public static BitmapSource BitmapToBitmapSource(Bitmap source)
    {
        return Imaging.CreateBitmapSourceFromHBitmap(
            source.GetHbitmap(), 
            IntPtr.Zero, 
            Int32Rect.Empty, 
            BitmapSizeOptions.FromEmptyOptions());
    }
    public static Bitmap SetBitmapResolution(Bitmap bitmap, float dpiX, float dpiY)
    {
        bitmap.SetResolution(dpiX, dpiY);
        return bitmap;
    }
    public static Bitmap Superimpose(Bitmap largeBmp, Bitmap smallBmp)
    {
        Graphics g = Graphics.FromImage(largeBmp);
        g.CompositingMode = CompositingMode.SourceOver;
        // smallBmp.MakeTransparent();
        int margin = 2;
        int x = largeBmp.Width - smallBmp.Width - margin;
        int y = largeBmp.Height - smallBmp.Height - margin;
        g.DrawImage(smallBmp, new Point(x, y));
        return largeBmp;
    }
    #endregion
}

呼叫代码

        string logoPath =  "watermark.png";
        var fs = new FileStream(path, FileMode.Create);
        BitmapSource bitmap = [LoadImage]
        if (Settings.Default.AddWaterMark)
        {
            BitmapSource logobitmap = new BitmapImage(new Uri(logoPath, UriKind.Absolute));
            Bitmap mainImgeBitmap = ImageHelper.BitmapSourceToBitmap(bitmap);
            Bitmap logoImageBitmap = ImageHelper.BitmapSourceToBitmap(logobitmap, ImageHelper.ImageType.PNG);
            logoImageBitmap = ImageHelper.SetBitmapResolution(
                logoImageBitmap,
                (float)bitmap.DpiX,
                (float)bitmap.DpiY);
            bitmap = ImageHelper.BitmapToBitmapSource(ImageHelper.Superimpose(mainImgeBitmap, logoImageBitmap));
        }
        var encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bitmap));
        encoder.Save(fs);
        fs.Close();