2010-11-06 21 views
11

Fotoğrafa özel bir yöntemle filigran eklemeliyim. Bunun nasıl yapılacağını biliyorum, ancak nasıl yapılır? http://www.photoshopessentials.com/photo-effects/copyright/C# - Fotoğrafa özel yolu ile filigran ekle

Burada filigran ekleme yöntemi vardır. Yukarıdaki makalede olduğu gibi filigran ile görüntü elde etmek için nasıl değiştirebilirim?

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

GÜNCELLEME

1 bağlantıların o saydam ve saydam bir arka plana sahip olduğunu böylece telif görüntüsünü oluşturursanız
2 bağlantıların geçerli bir sonuç expect result current result

+0

nasıl sonucudur senin Kod, makalede gösterilenlerden farklı mı? Tam olarak ne için yardım istiyorsun? –

+2

Bu kod gerçekten kötüydü. Tüm bu nesneleri “kov” un. – GSerg

+1

Farketmez. Algoritmaya ihtiyacım var. – Alexandre

cevap

14

sonucu bekliyoruz bunun gibi (Paint.NET kullanarak):

alt text

ondan bir TextureBrush oluşturmak ve orijinal görüntünün üzerine telif çizmek için kullanabilirsiniz:

bu sonucu üretir
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"); 
    } 
} 

:

alt text

+0

Çok çok iyi !!! "Using" kullanımı için – Alexandre

+0

+1. –

+0

İyi bir çözüm, teşekkürler – Riskhan