Reputation: 4430
I'm using Windows.UI is Winform in C#. I have file images in Photoshop; it was transparent. Like this:
So, I want the background to this picture; it will show. Example, I pick an orange colour.
I tried with:
gbmp.Clear(Color.Orange);
But it overrides my picture; the picture only has one colour is an orange.
My code to do this:
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);
I find this article like How to Change Pixel Color of an Image in C#.NET
Here is the Solution I have done with Pixels.
Attaching the source code so one can try the exact and get the result.
I have sample images of 128x128 (Width x Height).
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;
}
}
}
//Below is the sample image and different results by applying different colour
Code modifications will be highly appreciated.
It changes object in the image; it can't change the background.
Have you any idea to change this code?
Upvotes: 0
Views: 999
Reputation: 96
you are using this line :
gbmp.Clear(Color.Orange);
after load your img
using draw image maybe help you
first make a bitmap of size your img
Bitmap bmp = new Bitmap(width, height)
clear it as your colour :
bmp.Clear(Color.Orange);
then draw the img on bmp
also look this links, maybe help u :
Using Graphics.DrawImage() to Draw Image with Transparency/Alpha Channel
https://msdn.microsoft.com/en-us/library/8517ckds%28v=vs.110%29.aspx
Upvotes: 1