2014-12-09 7 views
0

큰 이미지에 투명한 워터 마크를 추가해야합니다. 모든 Google 검색으로 나는 지금까지 당신에게왔다. 코드가 제대로 작동하지 않는 것처럼 보이지만 물 이미지가 올바르게 배치되지 않고 왜곡되어 투명성이 떨어지는 것처럼 보입니다. 이 문제를 어떻게 해결할 수 있는지 알려주십시오. 이를 달성하는 더 좋은 방법이 있다면 알려주십시오.투명한 워터 마크 (png) 이미지를 다른 이미지에 추가

홈페이지 이미지 640 × 480

워터 마크 이미지 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(); 
+0

모두 비트 맵 같은 DPI 설정되어 있는지 확인; 그것을 설정함으로써 최고! – TaW

+0

반투명 비트 맵을 사용하여 불투명 비트 맵에 페인팅하면 불투명 비트 맵이 생성되어 eacj 픽셀의 투명도 수준에 따라 색상이 혼합됩니다. 반투명 워터 마크를 얻으려면 setpixel을 사용하여 픽셀을 변경해야합니다. 이러한 작은 워터 마크 이미지의 경우 큰 이슈가 아닙니다. 많은 이미지를 빠르게 변경해야하는 경우가 아니라면. 그러면 당신은 lockbit를 사용하고 싶어 할 것입니다. – TaW

답변

0

는 만들어 읽기 :) 필요가있는 한 것은 고정

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