Click here to Skip to main content
15,917,565 members
Home / Discussions / C#
   

C#

 
GeneralRe: Detaching Problem Pin
Xmen Real 14-Nov-07 0:16
professional Xmen Real 14-Nov-07 0:16 
GeneralRe: Detaching Problem Pin
Adeel Chaudhry14-Nov-07 0:22
Adeel Chaudhry14-Nov-07 0:22 
Questiondate fomats Pin
dandamudi padma13-Nov-07 17:04
dandamudi padma13-Nov-07 17:04 
AnswerRe: date fomats Pin
Christian Graus13-Nov-07 17:07
protectorChristian Graus13-Nov-07 17:07 
AnswerRe: date fomats Pin
Xmen Real 13-Nov-07 17:21
professional Xmen Real 13-Nov-07 17:21 
AnswerRe: date fomats Pin
Bino B13-Nov-07 21:33
Bino B13-Nov-07 21:33 
AnswerRe: date fomats Pin
DharmarajNagarajan14-Nov-07 1:20
DharmarajNagarajan14-Nov-07 1:20 
QuestionDirectly modify the bits of a bitmap Pin
Skippums13-Nov-07 15:39
Skippums13-Nov-07 15:39 
I am attempting to write my own flood fill algorithm, but it is proving to be pretty slow. I am wondering if anyone has an idea on how I can improve performance. Currently, I am using the 4-way non-recursive scanline algorithm, based on a Queue<Point>. I am using the WinAPI methods CreateCompatibleDC, GetPixel, SetPixel, and BitBlt. I have a feeling that if I can copy the bitmap to a byte array, then modify that array directly, then return the array to the bitmap, I can get better performance. Does anyone know how to do this, or have any other ideas as to how I can improve the following code? I copied my code below for examination (note that all the colors I am filling are gray, so R=G=B all the time, if it is a valid fill). Thanks,


private static int ToRGB(Color col) {
return (col.B << 16 | col.G << 8 | col.R);
}

private static bool AreEqual(ref int known, int other) {
if (known == other)
return true;
if ((other & 0xFF) != (other >> 8 & 0xFF) ||
(other & 0xFF) != (other >> 16 & 0xFF) ||
(other & 0xFF) < (known & 0xFF) - 2 ||
(other & 0xFF) > (known & 0xFF) + 2)
return false;
known = other;
return true;
}

private static bool FloodFill(Bitmap img, Point loc, Color fillColor) {
Graphics g = null;
IntPtr hdc1 = IntPtr.Zero, hdc2 = IntPtr.Zero;
try {
g = Graphics.FromImage(img);
hdc1 = g.GetHdc();
Color temp = ((Bitmap)img).GetPixel(loc.X, loc.Y);
hdc2 = WinAPI.CreateCompatibleDC(hdc1);
WinAPI.SelectObject(hdc2, img.GetHbitmap());
int oldColor = WinAPI.GetPixel(hdc2, loc.X, loc.Y);
if ((oldColor & 0xFF) != (oldColor >> 8 & 0xFF) ||
(oldColor & 0xFF) != (oldColor >> 16 & 0xFF))
return false;
Queue<Point> pixels = new Queue<Point>(128);
pixels.Enqueue(loc);
int x, y, newColor = ToRGB(fillColor);
bool spanL, spanR;
do {
loc = pixels.Dequeue();
x = loc.X;
y = loc.Y;
oldColor = WinAPI.GetPixel(hdc2, x, y);
while (y > 0) {
if (!AreEqual(ref oldColor, WinAPI.GetPixel(hdc2, x, --y))) {
++y;
break;
}
}
spanL = spanR = false;
do {
int res = WinAPI.SetPixel(hdc2, x, y, newColor);
if (!spanL) {
if (x > 0 && AreEqual(ref oldColor, WinAPI.GetPixel(hdc2, x - 1, y))) {
pixels.Enqueue(new Point(x - 1, y));
spanL = true;
}
} else if (!AreEqual(ref oldColor, WinAPI.GetPixel(hdc2, x - 1, y))) {
spanL = false;
}
if (!spanR) {
if (x < img.Width - 1 && AreEqual(ref oldColor, WinAPI.GetPixel(hdc2, x + 1, y))) {
pixels.Enqueue(new Point(x + 1, y));
spanR = true;
}
} else if (!AreEqual(ref oldColor, WinAPI.GetPixel(hdc2, x + 1, y))) {
spanR = false;
}
} while (++y < img.Height && AreEqual(ref oldColor, WinAPI.GetPixel(hdc2, x, y)));
} while (pixels.Count > 0);
WinAPI.BitBlockTransfer(hdc2, hdc1, 0, 0, 0, 0, img.Width, img.Height, WinAPI.TRO.SRCCOPY);
} catch {
return false;
} finally {
if (hdc2 != IntPtr.Zero)
WinAPI.DeleteDC(hdc2);
if (g != null) {
if (hdc1 != IntPtr.Zero)
g.ReleaseHdc(hdc1);
g.Dispose();
}
}
return true;
}

Jeff
AnswerRe: Directly modify the bits of a bitmap Pin
Christian Graus13-Nov-07 15:47
protectorChristian Graus13-Nov-07 15:47 
GeneralRe: Directly modify the bits of a bitmap Pin
Skippums14-Nov-07 19:20
Skippums14-Nov-07 19:20 
AnswerRe: Directly modify the bits of a bitmap Pin
Luc Pattyn13-Nov-07 15:55
sitebuilderLuc Pattyn13-Nov-07 15:55 
QuestionStored Procedure Pin
mojojojojo13-Nov-07 15:21
mojojojojo13-Nov-07 15:21 
AnswerRe: Stored Procedure Pin
Christian Graus13-Nov-07 15:48
protectorChristian Graus13-Nov-07 15:48 
GeneralRe: Stored Procedure Pin
mojojojojo13-Nov-07 16:38
mojojojojo13-Nov-07 16:38 
GeneralRe: Stored Procedure Pin
Christian Graus13-Nov-07 17:09
protectorChristian Graus13-Nov-07 17:09 
GeneralRe: Stored Procedure Pin
mojojojojo13-Nov-07 17:13
mojojojojo13-Nov-07 17:13 
AnswerRe: Stored Procedure Pin
Vasudevan Deepak Kumar13-Nov-07 20:17
Vasudevan Deepak Kumar13-Nov-07 20:17 
QuestionTo get the point(coordinate) of any control? Pin
omegazafer13-Nov-07 13:27
omegazafer13-Nov-07 13:27 
GeneralRe: To get the point(coordinate) of any control? Pin
omegazafer13-Nov-07 14:06
omegazafer13-Nov-07 14:06 
Questionusing dll created by MinGW in C# code Pin
dfn13-Nov-07 12:57
dfn13-Nov-07 12:57 
AnswerRe: using dll created by MinGW in C# code Pin
Judah Gabriel Himango13-Nov-07 13:01
sponsorJudah Gabriel Himango13-Nov-07 13:01 
QuestionTo show user's screen resolution ? Pin
omegazafer13-Nov-07 12:42
omegazafer13-Nov-07 12:42 
AnswerRe: To show user's screen resolution ? Pin
Christian Graus13-Nov-07 12:45
protectorChristian Graus13-Nov-07 12:45 
GeneralRe: To show user's screen resolution ? Pin
omegazafer13-Nov-07 12:54
omegazafer13-Nov-07 12:54 
GeneralRe: To show user's screen resolution ? Pin
Christian Graus13-Nov-07 12:58
protectorChristian Graus13-Nov-07 12:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.