2010-04-20 3 views
12

Ho un'immagine con un determinato motivo. Come posso ripeterlo in un'altra immagine usando GDI? Esiste un metodo per farlo in GDI?Ripeti l'immagine in C#

+0

Che tipo di motivo? Vuoi copiare i pixel? –

risposta

22

In C#, è possibile creare un TextureBrush che verrà affianca la tua immagine ovunque tu la usi, e poi riempia un'area con essa. Qualcosa di simile a questo (un esempio che riempie l'intera immagine) ...

// Use `using` blocks for GDI objects you create, so they'll be released 
// quickly when you're done with them. 
using (TextureBrush brush = new TextureBrush(yourImage, WrapMode.Tile)) 
using (Graphics g = Graphics.FromImage(destImage)) 
{ 
    // Do your painting in here 
    g.FillRectangle(brush, 0, 0, destImage.Width, destImage.Height); 
} 

nota, se volete qualche controllo su come l'immagine è piastrellato, si sta andando ad avere bisogno di imparare un po 'trasforma.

ho quasi dimenticato (in realtà ho dimenticato per un po '): Avrete bisogno di importare System.Drawing (per Graphics e TextureBrush) e System.Drawing.Drawing2D (per WrapMode) in modo che il codice di cui sopra a lavorare come è.

0

Non c'è alcuna funzione di dipingere una particolare immagine come un "modello" (pittura ripetutamente), ma dovrebbe essere abbastanza semplice da fare:

public static void FillPattern(Graphics g, Image image, Rectangle rect) 
{ 
    Rectangle imageRect; 
    Rectangle drawRect; 

    for (int x = rect.X; x < rect.Right; x += image.Width) 
    { 
     for (int y = rect.Y; y < rect.Bottom; y += image.Height) 
     { 
      drawRect = new Rectangle(x, y, Math.Min(image.Width, rect.Right - x), 
          Math.Min(image.Height, rect.Bottom - y)); 
      imageRect = new Rectangle(0, 0, drawRect.Width, drawRect.Height); 

      g.DrawImage(image, drawRect, imageRect, GraphicsUnit.Pixel); 
     } 
    } 
} 
+0

@sam: lo passi sul rettangolo dell'oggetto 'Graphics' che vuoi riempire con l'immagine. –