Getting the DoubleBuffered Bitmap
-
While drawing in a double buffered control, how would one get a hold of the actual Bitmap that .NET is holding for the buffering? So you could set individual pixels for example. Aside from using yet another bitmap to do all of the drawing, which is what double buffering is doing in the first place. Thanks
-
While drawing in a double buffered control, how would one get a hold of the actual Bitmap that .NET is holding for the buffering? So you could set individual pixels for example. Aside from using yet another bitmap to do all of the drawing, which is what double buffering is doing in the first place. Thanks
This is a double buffering code: (Its from Beginnning C# books by wrox publisher).Maybe it helps you:
Graphics displayGraphics = e.Graphics;
Random r = new Random();
Image i = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
Graphics g = Graphics.FromImage(i);
g.FillRectangle(Brushes.White, ClientRectangle);for (int x = 0; x < ClientRectangle.Width; x++)
{
for (int y = 0; y < ClientRectangle.Height; y += 10)
{
Color c = Color.FromArgb (r.Next(255), r.Next(255),
r.Next(255));
Pen p = new Pen(c, 1);
g.DrawLine(p, new Point(0, 0), new Point(x, y));
p.Dispose();
}
}
displayGraphics.DrawImage(i, ClientRectangle);
i.Dispose();Mazy "The path you tread is narrow and the drop is shear and very high, The ravens all are watching from a vantage point near by, Apprehension creeping like a choo-train uo your spine, Will the tightrope reach the end;will the final cuplet rhyme?"Cymbaline-Pink Floyd
-
This is a double buffering code: (Its from Beginnning C# books by wrox publisher).Maybe it helps you:
Graphics displayGraphics = e.Graphics;
Random r = new Random();
Image i = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
Graphics g = Graphics.FromImage(i);
g.FillRectangle(Brushes.White, ClientRectangle);for (int x = 0; x < ClientRectangle.Width; x++)
{
for (int y = 0; y < ClientRectangle.Height; y += 10)
{
Color c = Color.FromArgb (r.Next(255), r.Next(255),
r.Next(255));
Pen p = new Pen(c, 1);
g.DrawLine(p, new Point(0, 0), new Point(x, y));
p.Dispose();
}
}
displayGraphics.DrawImage(i, ClientRectangle);
i.Dispose();Mazy "The path you tread is narrow and the drop is shear and very high, The ravens all are watching from a vantage point near by, Apprehension creeping like a choo-train uo your spine, Will the tightrope reach the end;will the final cuplet rhyme?"Cymbaline-Pink Floyd
While I appreciate the answer, please read the question before answering it… ;-) The .NET Control already has the ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint styles set, which provides double buffering for the control. As a mater a fact, I am already doing something like what you posted (on top of double buffering) to get to any kind of SetPixel method. What I would like to know is if I can get a hold of the bitmap .NET already has, for the double buffering I told it to do? Thanks
-
While I appreciate the answer, please read the question before answering it… ;-) The .NET Control already has the ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint styles set, which provides double buffering for the control. As a mater a fact, I am already doing something like what you posted (on top of double buffering) to get to any kind of SetPixel method. What I would like to know is if I can get a hold of the bitmap .NET already has, for the double buffering I told it to do? Thanks
Neil Van Note wrote: While I appreciate the answer, please read the question before answering it… Ok,I'll do it.:) Neil Van Note wrote: The .NET Control already has the ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint styles set, which provides double buffering for the control. I didn't know this one,thanks. Mazy "The path you tread is narrow and the drop is shear and very high, The ravens all are watching from a vantage point near by, Apprehension creeping like a choo-train uo your spine, Will the tightrope reach the end;will the final cuplet rhyme?"Cymbaline-Pink Floyd
-
While drawing in a double buffered control, how would one get a hold of the actual Bitmap that .NET is holding for the buffering? So you could set individual pixels for example. Aside from using yet another bitmap to do all of the drawing, which is what double buffering is doing in the first place. Thanks
I looked through the docs and nothing obvious stuck out at me. I even tried doing some p/invoke but got nowhere. It looks like you can't get the underlying bitmap unless you create it yourself :( James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971
-
I looked through the docs and nothing obvious stuck out at me. I even tried doing some p/invoke but got nowhere. It looks like you can't get the underlying bitmap unless you create it yourself :( James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971
That’s what I found, I was hopping I was wrong... So this leads be to my second question. Is there a method that I am not seeing, that will allow me to set an individual pixel, other than those of the Bitmap’s? I am already creating a Bitmap to achieve this, I was just hopping there was a more direct method. Again, nothing in the documentation is jumping out at me. Thanks
-
That’s what I found, I was hopping I was wrong... So this leads be to my second question. Is there a method that I am not seeing, that will allow me to set an individual pixel, other than those of the Bitmap’s? I am already creating a Bitmap to achieve this, I was just hopping there was a more direct method. Again, nothing in the documentation is jumping out at me. Thanks
Nothing directly unfortunately, you might be able to use DrawLine to simulate it. It really looks like GDI+ (ie the System.Drawing namespace) was meant for higher level operations. You can make a call out to gdi32.dll with p/invoke, but that could possibly be a real performance drag. I'll come up with a sample and compare the results of p/invoke and Bitmap.SetPixel :) James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971
-
Nothing directly unfortunately, you might be able to use DrawLine to simulate it. It really looks like GDI+ (ie the System.Drawing namespace) was meant for higher level operations. You can make a call out to gdi32.dll with p/invoke, but that could possibly be a real performance drag. I'll come up with a sample and compare the results of p/invoke and Bitmap.SetPixel :) James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971
>> "you might be able to use DrawLine to simulate it" :( Haaa, if it comes down to that, I'll stick with the Bitmap, the performance is actually pretty good considering I am keeping it around for the life of the control. P/Invoke can be a good and bad thing, the number of Sets are numerous and again, I would rather use the extra Bitmap, than to call out to SetPixelV 1000 times. I would be curious to see what your performance test show. Regards
-
>> "you might be able to use DrawLine to simulate it" :( Haaa, if it comes down to that, I'll stick with the Bitmap, the performance is actually pretty good considering I am keeping it around for the life of the control. P/Invoke can be a good and bad thing, the number of Sets are numerous and again, I would rather use the extra Bitmap, than to call out to SetPixelV 1000 times. I would be curious to see what your performance test show. Regards
I've created a benchmark program, VS.NET project and batch file provided to compile it. GDIvsGraphics.zip James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971
-
I've created a benchmark program, VS.NET project and batch file provided to compile it. GDIvsGraphics.zip James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971
Interesting results, while the internal Bitmap.SetPixel is still the clear winner, The PInvoke performance is not as bad as I thought is was going to be. Regards
-
Interesting results, while the internal Bitmap.SetPixel is still the clear winner, The PInvoke performance is not as bad as I thought is was going to be. Regards
Whats even better is that the Graphics version is slowed down a little by calling clr.ToARGB() each iteration even though it isn't used. This was done so the comparison would be in using P/Invoke instead of P/Invoke and the needed call to ToARGB(). James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971
-
Whats even better is that the Graphics version is slowed down a little by calling clr.ToARGB() each iteration even though it isn't used. This was done so the comparison would be in using P/Invoke instead of P/Invoke and the needed call to ToARGB(). James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971
True, Normally I wouldn't do such things in a benchmark; I feel benchmarks like this should only perform the work necessary to achieve the same end result, and not equivalent processing. I massaged the code a little and ran it under DevPartner Profiler to obtain (for my actual scenario) more accurate results. Thanks and Regards