0

I have a 256x256 .ico that I want to print through my C# syntax. This is my syntax

Image logoImage = global::Winform1.Properties.Resources.KA0_icon.ToBitmap();
Rectangle LogoRect = new Rectangle(m_leftMargin, m_leftMargin, (int)(logoImage.Width * 0.75), (int)(logoImage.Height * 0.8));
e.Graphics.DrawImage(logoImage, LogoRect);
e.Graphics.DrawRectangle(Pens.LightBlue, LogoRect);

But this throws an error of:

An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code
Additional information: Requested range extends past the end of the array

What must I do in order to have this .ico file display on the top of the page I am printing?

Edit
Per the suggestions below I have also tried this syntax

Image logoImage = Bitmap.FromHicon(global::Winform1.Properties.Resources.KA0_icon, new Size(48, 48).Handle);

however this gives me an error of

Size' does not contain a definition for 'Handle' and no extension method 'Handle' accepting a first argument of type 'Size' could be found (are you missing a using directive or an assembly reference?)

Smith Stanley
  • 461
  • 1
  • 8
  • 25
  • You might have to share your KA0_icon with us to reproduce the error you are getting. – LarsTech Aug 28 '17 at 17:50
  • @LarsTech - SO will not allow me to upload a file with a .ico type. Thoughts? – Smith Stanley Aug 28 '17 at 18:01
  • Possible duplicate of [Displaying an icon in a picturebox](https://stackoverflow.com/questions/15782857/displaying-an-icon-in-a-picturebox) – Teneko Aug 28 '17 at 18:11
  • Icon.ToBitmap() is a bit lame, you'd have to target .NET 4.6 or higher to avoid this exception. It just isn't the smart way to do this, use the Graphics.DrawIcon() method instead. Also gives you much better odds of not forgetting to use the *using* statement, the icon should be disposed. – Hans Passant Aug 28 '17 at 18:19
  • @HansPassant - can you provide a link to an example or syntax to follow? – Smith Stanley Aug 28 '17 at 18:20
  • I can't imagine what link you'd need, if you know how to call DrawImage then you know how to call DrawIcon. All you can do wrong is not trying it. – Hans Passant Aug 28 '17 at 18:23

2 Answers2

1

Have a look at this similar question:

Displaying an icon in a picturebox

It mentions the same exception. To do the conversion, you may have success doing something like that:

Bitmap.FromHicon(global::Winform1.Properties.Resources.KA0_icon.Handle);

Or possibly:

Bitmap.FromHicon(new Icon(global::Winform1.Properties.Resources.KA0_icon, new Size(256, 256)).Handle);
drone6502
  • 433
  • 2
  • 7
0

You have to debug. Check if your image where you are intended to daw is big enough. An ArgumentOutOfRangeException means, that you are doing stuff that's not in range, so just look how big your image, icon and rectangle is and compare.

And a hint: You should consider to write (int)(logoImage.Width * 0.75f) that's what I've learned, because I got sometimes unreliable values.

Teneko
  • 1,417
  • 9
  • 16