2016-05-26 11 views
0

Ich benutze Windows.UI ist Winform in C#. Ich habe Dateibilder in Photoshop; es war transparent. Wie folgt aus:Wie kann man Bitmap mit Hintergrund in C# speichern?

Image transparent

Also, ich möchte den Hintergrund dieses Bild; wird sich zeigen. Beispiel, ich wähle eine orange Farbe.

Image with background

Ich habe versucht, mit:

gbmp.Clear(Color.Orange);

Aber es überschreibt mein Bild; Das Bild hat nur eine Farbe ist eine Orange.

Mein Code, dies zu tun:

Graphics gra = Graphics.FromImage(img); 

Bitmap bmp = new Bitmap(@"" + pathToFile); 
panel2.BackgroundImage = bmp; 
Graphics gbmp = Graphics.FromImage(bmp); 
gbmp.Clear(Color.Orange); 

gbmp.DrawImage(
DrawText("WHAT UP?", fontType, myColorLabel1, 
    Color.Transparent), 
Point.Round(StretchImageSize(new Point(activeLabels[1].Location.X, activeLabels[1].Location.Y), panel2))); 
gra.Dispose(); 
Guid id = Guid.NewGuid(); 
ScaleImage(bmp, witdhImg, heightImg) 
    .Save(linkLocation + "\\" + id + "." + imgType, 
     ImageFormat.Png); 

ich diesen Artikel finden wie How to Change Pixel Color of an Image in C#.NET

Hier ist die Lösung I mit Pixel getan haben.

Anfügen des Quellcodes, so kann man das genaue versuchen und das Ergebnis erhalten.

Ich habe Beispielbilder von 128x128 (Breite x Höhe).

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Drawing; 
using System.IO; 
//using System.Globalization; 

namespace colorchange 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      try 
      { 
       Bitmap bmp = null; 
       //The Source Directory in debug\bin\Big\ 
       string[] files = Directory.GetFiles("Big\\"); 
       foreach (string filename in files) 
       { 
       bmp = (Bitmap)Image.FromFile(filename);      
       bmp = ChangeColor(bmp); 
       string[] spliter = filename.Split('\\'); 
       //Destination Directory debug\bin\BigGreen\ 
       bmp.Save("BigGreen\\" + spliter[1]); 
       }             
      } 
      catch (System.Exception ex) 
      { 
       Console.WriteLine(ex.ToString()); 
      }    
     }   
     public static Bitmap ChangeColor(Bitmap scrBitmap) 
     { 
      //You can change your new color here. Red,Green,LawnGreen any.. 
      Color newColor = Color.Red; 
      Color actualColor;    
      //make an empty bitmap the same size as scrBitmap 
      Bitmap newBitmap = new Bitmap(scrBitmap.Width, scrBitmap.Height); 
      for (int i = 0; i < scrBitmap.Width; i++) 
      { 
      for (int j = 0; j < scrBitmap.Height; j++) 
      { 
       //get the pixel from the scrBitmap image 
       actualColor = scrBitmap.GetPixel(i, j); 
       // > 150 because.. Images edges can be of low pixel colr. if we set all pixel color to new then there will be no smoothness left. 
       if (actualColor.A > 150) 
        newBitmap.SetPixel(i, j, newColor); 
       else 
        newBitmap.SetPixel(i, j, actualColor); 
      } 
      }    
      return newBitmap; 
     } 
    } 
} 

// Unten ist das Beispielbild und unterschiedliche Ergebnisse durch unterschiedliche Farb Anwendung

Modifikationen-Code wird sehr geschätzt.

Es ändert Objekt im Bild; Es kann den Hintergrund nicht ändern.

Haben Sie eine Idee, diesen Code zu ändern?

+1

Sie können 'SetPixel' und' GetPixel' auf das Bild . Wenn 'GetPixel' mit der Farbe übereinstimmt, dann benutze 'SetPixel' um es transparent zu machen – Bauss

+0

@Bauss Ich habe meine Frage aktualisiert. Kannst du dieses Problem noch einmal sehen? – vanloc

Antwort

Verwandte Themen