Coloring pictures
-
Hello I'm using the below code to color a picture but it is pretty slow is there a way to do it faster? Thanks in advance.
Bitmap Turn(Bitmap pic) { Bitmap newpic = new Bitmap(pic.Width, pic.Height); for (int i = 0; i < pic.Width; i++) { for (int j = 0; j < pic.Height; j++) { Color color = pic.GetPixel(i, j); newpic.SetPixel(i, j, Color.FromArgb(123, 46, 125)); } } return newpic; }
-
Hello I'm using the below code to color a picture but it is pretty slow is there a way to do it faster? Thanks in advance.
Bitmap Turn(Bitmap pic) { Bitmap newpic = new Bitmap(pic.Width, pic.Height); for (int i = 0; i < pic.Width; i++) { for (int j = 0; j < pic.Height; j++) { Color color = pic.GetPixel(i, j); newpic.SetPixel(i, j, Color.FromArgb(123, 46, 125)); } } return newpic; }
Why are you retrieving the color from pic when you don't use it? Here
Color color = pic.GetPixel(i, j); <=============================== this never gets used.
newpic.SetPixel(i, j, Color.FromArgb(123, 46, 125));But if your newpic is to be all one color, look up the
Graphics
class, and theFill
method of that class.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Hello I'm using the below code to color a picture but it is pretty slow is there a way to do it faster? Thanks in advance.
Bitmap Turn(Bitmap pic) { Bitmap newpic = new Bitmap(pic.Width, pic.Height); for (int i = 0; i < pic.Width; i++) { for (int j = 0; j < pic.Height; j++) { Color color = pic.GetPixel(i, j); newpic.SetPixel(i, j, Color.FromArgb(123, 46, 125)); } } return newpic; }
Search the articles for "Image processing for dummies" and you'll find a series of articles that covers just what you're looking for.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
Hello I'm using the below code to color a picture but it is pretty slow is there a way to do it faster? Thanks in advance.
Bitmap Turn(Bitmap pic) { Bitmap newpic = new Bitmap(pic.Width, pic.Height); for (int i = 0; i < pic.Width; i++) { for (int j = 0; j < pic.Height; j++) { Color color = pic.GetPixel(i, j); newpic.SetPixel(i, j, Color.FromArgb(123, 46, 125)); } } return newpic; }
Hi, 1. an easy win is to replace pic.Height by a variable that gets initialized only once. 2. for a fixed color, there are better ways, as Henry already mentioned. Here is one:
Graphics g=Graphics.FromImage(newpic);
g.FillRectangle(...);
g.Dispose();3. the general solution for maximum performance is using pointers, avoiding GetPixel/SetPixel since these methods will perform boundary checks and coordinate conversions for each individual pixel. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Hello I'm using the below code to color a picture but it is pretty slow is there a way to do it faster? Thanks in advance.
Bitmap Turn(Bitmap pic) { Bitmap newpic = new Bitmap(pic.Width, pic.Height); for (int i = 0; i < pic.Width; i++) { for (int j = 0; j < pic.Height; j++) { Color color = pic.GetPixel(i, j); newpic.SetPixel(i, j, Color.FromArgb(123, 46, 125)); } } return newpic; }
I have realised that I made a typo, had a brain fart, in my previous post. I should have said If it is going to be one color investigate the
Graphics
class and theClear
method of that class. Although the variousFill
methods are good to know.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
I have realised that I made a typo, had a brain fart, in my previous post. I should have said If it is going to be one color investigate the
Graphics
class and theClear
method of that class. Although the variousFill
methods are good to know.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”