2009-01-21 18 views
68

Einfache Idee: Ich habe zwei Bilder, die ich zusammenführen möchte, ist eine 500x500, die in der Mitte transparent ist, die andere ist 150x150.Zusammenfügen von zwei Bildern in C# /. NET

Grundidee ist dies: Erstellen Sie eine leere Leinwand, die 500x500 ist, positionieren Sie das 150x150 Bild in der Mitte der leeren Leinwand und kopieren Sie dann das 500x500 Bild, so dass die transparente Mitte der 150x150 durchscheinen kann.

Ich weiß, wie man es in Java, PHP und Python macht ... Ich habe einfach keine Ahnung, welche Objekte/Klassen in C# verwendet werden, ein schnelles Beispiel für das Kopieren von Bildern in ein anderes wäre ausreichend.

+0

Hilft das? http://www.daniweb.com/forums/thread87993.html – Dror

Antwort

85

im Grunde das i in einem unserer Apps nutzen:

Image playbutton; 
try 
{ 
    playbutton = Image.FromFile(/*somekindofpath*/); 
} 
catch (Exception ex) 
{ 
    return; 
} 

Image frame; 
try 
{ 
    frame = Image.FromFile(/*somekindofpath*/); 
} 
catch (Exception ex) 
{ 
    return; 
} 

using (frame) 
{ 
    using (var bitmap = new Bitmap(width, height)) 
    { 
     using (var canvas = Graphics.FromImage(bitmap)) 
     { 
      canvas.InterpolationMode = InterpolationMode.HighQualityBicubic; 
      canvas.DrawImage(frame, 
          new Rectangle(0, 
              0, 
              width, 
              height), 
          new Rectangle(0, 
              0, 
              frame.Width, 
              frame.Height), 
          GraphicsUnit.Pixel); 
      canvas.DrawImage(playbutton, 
          (bitmap.Width/2) - (playbutton.Width/2), 
          (bitmap.Height/2) - (playbutton.Height/2)); 
      canvas.Save(); 
     } 
     try 
     { 
      bitmap.Save(/*somekindofpath*/, 
         System.Drawing.Imaging.ImageFormat.Jpeg); 
     } 
     catch (Exception ex) { } 
    } 
} 
+9

DANKE! Totally gerettet mein Speck heute –

+0

@Downvoter Pflege zu erarbeiten, so dass ich meine Antwort verbessern kann? –

+3

@AndreasNiedermair die down-Voter kopieren wahrscheinlich eingefügt Ihren Code und hat nicht funktioniert –

50

Dies wird ein Bild zu einem anderen hinzufügen.

using (Graphics grfx = Graphics.FromImage(image)) 
{ 
    grfx.DrawImage(newImage, x, y) 
} 

Graphics ist im Namensraum System.Drawing

22

Denn: wir eine PlayIcon über einen Rahmen eines Videos überlagern wollen dies, ich fand eine neue einfachere Methode versuchen, dies ..

Es kann mehrere Fotos zusammen:

public static System.Drawing.Bitmap CombineBitmap(string[] files) 
{ 
    //read all images into memory 
    List<System.Drawing.Bitmap> images = new List<System.Drawing.Bitmap>(); 
    System.Drawing.Bitmap finalImage = null; 

    try 
    { 
     int width = 0; 
     int height = 0; 

     foreach (string image in files) 
     { 
      //create a Bitmap from the file and add it to the list 
      System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image); 

      //update the size of the final bitmap 
      width += bitmap.Width; 
      height = bitmap.Height > height ? bitmap.Height : height; 

      images.Add(bitmap); 
     } 

     //create a bitmap to hold the combined image 
     finalImage = new System.Drawing.Bitmap(width, height); 

     //get a graphics object from the image so we can draw on it 
     using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImage)) 
     { 
      //set background color 
      g.Clear(System.Drawing.Color.Black); 

      //go through each image and draw it on the final image 
      int offset = 0; 
      foreach (System.Drawing.Bitmap image in images) 
      { 
       g.DrawImage(image, 
        new System.Drawing.Rectangle(offset, 0, image.Width, image.Height)); 
       offset += image.Width; 
      } 
     } 

     return finalImage; 
    } 
    catch (Exception ex) 
    { 
     if (finalImage != null) 
      finalImage.Dispose(); 

     throw ex; 
    } 
    finally 
    { 
     //clean up memory 
     foreach (System.Drawing.Bitmap image in images) 
     { 
      image.Dispose(); 
     } 
    } 
} 
+2

hat super funktioniert. g.Clear (Color.Transparent), wenn PNG-Bilder für Animations-Sprites zusammengeführt werden sollen – syclee

+1

finalImage = new System.Drawing.Bitmap (width, height); wirft Fehler für hohe Werte von Breite/Höhe – zeetit

+0

@Anant Dabhi Okay es tut mir leid, eine alte Frage zurück zu bringen, aber ich konvertierte das zu VB.NET. Wird das andere Fotos überlagern, wenn ich sie übereinander platziere, wenn die unbenutzten Pixel/leere Pixel auf dem nächsten Bild ist transparent? Wenn nicht, gibt es einen Weg, es zu tun? – Speentie8081