URGENT: ArgumentException on creating bitmap

M

mrabie

Hi All,

I am writing a custom control and that i create a graphics buffer for
when it's initializing. the code is as follows

private void CreateBackBuffer()
{
CleanupBackBuffer();

m_backBufferBitmap = new Bitmap(Bounds.Width,
Bounds.Height);
m_backBuffer = Graphics.FromImage(m_backBufferBitmap);
}

when i try to run the application i get the following error @
m_backBufferBitmap = new Bitmap(Bounds.Width, Bounds.Height);

System.ArgumentException was unhandled
Message="Value does not fall within the expected range."
StackTrace:
at Microsoft.AGL.Common.MISC.HandleAr()
at System.Drawing.Bitmap._Init()
at System.Drawing.Bitmap..ctor()
at testklist.KListControl.CreateBackBuffer()
at testklist.KListControl..ctor()
at testklist.Form1.InitializeComponent()
at testklist.Form1..ctor()
at testklist.Program.Main()
InnerException:

The values for bounds are
Location: 0,0
Width = 150
Height = 150

i am using VS 2008 .Net 3.5 platoform WM6

Any ideas why this is happening or how can i fix it would be really
appreciated.

Thanks for your help in advance
 
C

Chris Tacke, eMVP

1. Don't tell us it's urgent. Ever. I don't care if you think it's urgent.
Most people who post a question think their question is urgent - it's not
like they post and think "sure, an answer next month would be fine." It's
not urgent for us to answer, and trying to tell us it's urgent actually
makes many people less inclined to answer.

2. Pardon me, but I don't believe you. If you are passing in 150 and 150,
it should work they are valid ranged values. Have you tried passing in
hard-coded values of 150 for width and height into the constructor to see if
those work? If they do, then the Bounds values are not what you think they
are. How did you verify these? You might add some checks like an Assert or
a Debug.WriteLine to verify the values.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 
T

The CyberKnight

We do have a similar issue here where

using (Graphics sourceGraphic = Graphics.FromImage(image))
{
IntPtr sourceHDC = sourceGraphic.GetHdc();
...
}

actually throws a [System.ArgumentException] = {"Value does not fall within
the expected range."} sporadically (about once every 15 times). The bitmap is
valid and its size is a respectable 190 x 60 (nothing exceptional there).
Every other parameter is within normal range.

The project is also compiled for WM6 with the .NET framework 3.5 under VS2008.
Very suspicious.
 
R

rfernandez

I experienced something similar, but only for JPG images. The Imaging
API raises an exception from time to time, probably due to memory
management issues in the JPG decoder. In order to prevent it, I had to
create a DDB object, release the original DIB object and perform the
drawing operations using the recently created object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top