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

时间:2014-12-09 10:41:52

标签: c# image watermark

我需要为更大的图像添加透明水印。随着所有的谷歌搜索我到目前为止来到你 代码似乎工作,但水图像没有正确放置,似乎扭曲和失去透明度。请让我知道如何纠正这个问题。如果有更好的方法来实现这一点,请告诉我。

主图像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();

1 个答案:

答案 0 :(得分:1)

修复了任何需要阅读的人:)

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();