C# 将透明水印(png)图像添加到其他图像
我需要添加一个透明水印到一个更大的图像。通过谷歌搜索,我找到了你 代码似乎可以工作,但水的图像没有被正确放置,似乎扭曲和失去透明度。请告诉我如何纠正这个问题。如果有更好的方法来实现这一点,请让我知道 主图像640x480 水印图像100x86C# 将透明水印(png)图像添加到其他图像,c#,image,watermark,C#,Image,Watermark,我需要添加一个透明水印到一个更大的图像。通过谷歌搜索,我找到了你 代码似乎可以工作,但水的图像没有被正确放置,似乎扭曲和失去透明度。请告诉我如何纠正这个问题。如果有更好的方法来实现这一点,请让我知道 主图像640x480 水印图像100x86 public static class ImageHelper { #region Public Methods and Operators public static Bitmap BitmapSourceToBitmap(Bitmap
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();
为任何需要它的人修复了它(请阅读:) 呼叫码
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();
确保两个位图具有相同的dpi设置;最好设置它!在不透明位图上使用半透明位图绘制将生成不透明位图,根据每个像素的透明度级别混合颜色。如果你想得到一个半透明的水印,你需要用setpixel改变像素。对于这样一个小的水印图像不是一个大问题,除非您需要快速更改很多图像。那你就要用锁具了。。
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();