"Parameter is not valid." when drawing image

T

trant

I am using DrawImageUnscaled to draw a rendered image to my UI and everything
works fine for everything except 1 case where the image is really wide (68420
x 428)

the code is just:

g.DrawImageUnscaled(img, this.AutoScrollPosition.X,
this.AutoScrollPosition.Y, this.ClientSize.Width, this.ClientSize.Height);

and the img is a Bitmap image. There were no problems creating the img and
it is created with the same code that created all the other images that work
fine.

I guess it is something to do with the size.

If so, is there any way to draw this image even if its so large?
 
P

Peter Duniho

trant said:
I am using DrawImageUnscaled to draw a rendered image to my UI and everything
works fine for everything except 1 case where the image is really wide (68420
x 428)

Is it really only when it's as wide as 68420 pixels? What is the real
cut-off point of the problem?

If the cut-off is at exactly 32768 pixels wide (max 16-bit signed int
plus one), then you have a coordinate system problem. Otherwise, it's
more likely there's just a memory allocation issue.

Hard to say without a concise-but-complete code example that reliably
demonstrates the problem. But some kind of integer limit seems more
likely to me. 68420 x 428 is less than 112MB of pixel data, even for a
32bpp image. It's big, but not so enormous a memory allocation failure
ought to occur (at least, not unless there's a bunch of other huge
memory allocations going on).
the code is just:

g.DrawImageUnscaled(img, this.AutoScrollPosition.X,
this.AutoScrollPosition.Y, this.ClientSize.Width, this.ClientSize.Height);

and the img is a Bitmap image. There were no problems creating the img and
it is created with the same code that created all the other images that work
fine.

I guess it is something to do with the size.

If so, is there any way to draw this image even if its so large?

Well, the obvious work-around is to copy the part of the image you want
to draw into a smaller bitmap and draw that.

Another option may be to try using the DrawImage() instead; the
"unscaled" version is mainly just a performance optimization, and IMHO
an out-of-date one (even the scaled version should be smart enough to
bypass the scaling code when the actual output isn't really scaled).
(For that matter, I know of at least one bug in .NET/GDI+ where scaling
the image just a tiny bit results it _better_ performance).

If you want a more detailed reply or advice, you need to post a
concise-but-complete code example that reliably demonstrates the problem.

Pete
 
T

trant

Apparently using the regular Graphics object and Bitmaps is not good
according to some articles, but I have no idea how to use the alternatives.
Some GDI+ or Widnows.Media package?

Can anyone suggest which direction I should go?

My program draws out 2D flowcharts which can get really large. Obviously
standard Graphics cannot handle them so I need something better. What should
I go for?
 
P

Peter Duniho

trant said:
Apparently using the regular Graphics object and Bitmaps is not good
according to some articles,

According to what articles? The Graphics and Bitmap classes are core
data structures in .NET. Almost every .NET program is broken if using
them "is not good".
but I have no idea how to use the alternatives.
Some GDI+ or Widnows.Media package?

The core .NET implementation is built on GDI+. I would be surprised if
even WPF (System.Windows.Media) is completely independent of GDI+.
Can anyone suggest which direction I should go? [...]

Step 1: actually diagnose your problem. What is _really_ breaking?

You can't expect to come up with the correct fix until you know what the
problem is.

Pete
 
T

trant

Pete,

Thank you very much for your reply and advice!

Your suggestion for cropping the image prior to drawing it worked perfectly,
although I still do not quite understand why...

The image dimensions were not larger than int.MaxSize and everthing I draw
uses int for positioning, that remains the same. Yet now I just crop the
image to size and it works.

Thanks again!
 

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