简体   繁体   中英

Print An Image On Page With C#

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?)

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM