2009-01-21 8 views
68

Idée simple: J'ai deux images que je veux fusionner, l'une est 500x500 qui est transparente au milieu et l'autre 150x150. L'idée de base est la suivante: Créez un canevas vide de 500x500, positionnez l'image 150x150 au milieu du canevas vide, puis copiez l'image 500x500 afin que le milieu transparent permette au 150x150 de briller.Fusionner deux images en C# /. NET

Je sais comment le faire en Java, PHP et Python ... Je n'ai aucune idée des objets/classes à utiliser en C#, un exemple rapide de copie d'images dans un autre suffirait.

+0

Cela aide-t-il? http://www.daniweb.com/forums/thread87993.html – Dror

Répondre

85

essentiellement i utiliser dans une de nos applications: nous voulons superposer une PlayIcon sur un cadre d'une vidéo:

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

MERCI! Totalement sauvé mon lard aujourd'hui –

+0

@downvoter soin d'élaborer, de sorte que je peux améliorer ma réponse? –

+3

@AndreasNiedermair l'abaisseur probablement copier copier votre code et n'a pas fonctionné –

50

Cela va ajouter une image à l'autre.

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

Graphics est dans l'espace de noms System.Drawing

22

Après tout ceci, j'ai trouvé une nouvelle méthode plus facile essayer ceci ..

Il peut joindre plusieurs photos ensemble:

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

a bien fonctionné. g.Clear (Color.Transparent) si vous souhaitez fusionner des images PNG pour des sprites d'animation – syclee

+1

finalImage = new System.Drawing.Bitmap (width, height); jette une erreur pour des valeurs élevées de largeur/hauteur – zeetit

+0

@Anant Dabhi Ok Je suis désolé de ramener une vieille question, mais je l'ai convertie en VB.NET .. Est-ce que cela superposera d'autres photos si je les place les unes sur les autres si les pixels inutilisés/les pixels vides sur l'image suivante sont-ils transparents? Sinon, y a-t-il un moyen de le faire? – Speentie8081

Questions connexes