473,748 Members | 2,540 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Icon corruption when loading from a resource

When I display icons in controls from an imagelist, the icons are
"corrupted" if they are added from a resource (or a file) but are fine if I
add them using the designer at design time. I need to add the icons
dynamically at runtime.

How can you add the icons at runtime without corrupting the image?
Jul 21 '05 #1
4 2021
How are you adding them?
In what way are they corrupted?

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"Carl Scarlett" <ab*@de.f.g> wrote in message
news:OF******** ******@TK2MSFTN GP12.phx.gbl...
When I display icons in controls from an imagelist, the icons are
"corrupted" if they are added from a resource (or a file) but are fine if
I
add them using the designer at design time. I need to add the icons
dynamically at runtime.

How can you add the icons at runtime without corrupting the image?

Jul 21 '05 #2
Here's some code from my test project. I have an icon "help2.ico" that
contains all possible image sizes and formats. At design time, I add it to
ImageList1. I also create ListView1 with 3 items in it, and add the icon to
the project as an embedded resource.

When you run the project, the last two items have corrupted icons. It looks
like the icons are being resized from a larger icon size. How do I tell it
to use the correct icon size from the icon?

Code follows:
--------------

Private Sub Form1_Load(ByVa l sender As Object, ByVal e As
System.EventArg s) Handles MyBase.Load

'Set image list
ListView1.Small ImageList = ImageList1

'Set item 0 image
ListView1.Items (0).ImageIndex = 0

'Load from resource
ImageList1.Imag es.Add(New
System.Drawing. Icon(System.Ref lection.Assembl y.GetExecutingA ssembly.GetMani f
estResourceStre am(Me.GetType, "help2.ico" )))

'Set item 1 image
ListView1.Items (1).ImageIndex = ImageList1.Imag es.Count - 1

'Load from file
Dim s As String = Application.Sta rtupPath
Dim FileName As String = s.Substring(0, s.LastIndexOf(" \"c) + 1) &
"help2.ico"
ImageList1.Imag es.Add(New System.Drawing. Icon(FileName))

'Set item 2 image
ListView1.Items (2).ImageIndex = ImageList1.Imag es.Count - 1

End Sub

"Mick Doherty"
<EX***********@ AND.REMOVE.SQUA REBRACKETS.[mdaudi100#ntlwo rld.com]> wrote in
message news:eK******** ******@tk2msftn gp13.phx.gbl...
How are you adding them?
In what way are they corrupted?

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"Carl Scarlett" <ab*@de.f.g> wrote in message
news:OF******** ******@TK2MSFTN GP12.phx.gbl...
When I display icons in controls from an imagelist, the icons are
"corrupted" if they are added from a resource (or a file) but are fine if I
add them using the designer at design time. I need to add the icons
dynamically at runtime.

How can you add the icons at runtime without corrupting the image?


Jul 21 '05 #3
The class at the following link will help you to extract a specific icon
from the Icon Resource.
http://www.codeproject.com/dotnet/MultiIcon.asp

Your code looks fine.

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"Carl Scarlett" <ab*@de.f.g> wrote in message
news:ew******** ******@TK2MSFTN GP10.phx.gbl...
Here's some code from my test project. I have an icon "help2.ico" that
contains all possible image sizes and formats. At design time, I add it
to
ImageList1. I also create ListView1 with 3 items in it, and add the icon
to
the project as an embedded resource.

When you run the project, the last two items have corrupted icons. It
looks
like the icons are being resized from a larger icon size. How do I tell
it
to use the correct icon size from the icon?

Code follows:
--------------

Private Sub Form1_Load(ByVa l sender As Object, ByVal e As
System.EventArg s) Handles MyBase.Load

'Set image list
ListView1.Small ImageList = ImageList1

'Set item 0 image
ListView1.Items (0).ImageIndex = 0

'Load from resource
ImageList1.Imag es.Add(New
System.Drawing. Icon(System.Ref lection.Assembl y.GetExecutingA ssembly.GetMani f
estResourceStre am(Me.GetType, "help2.ico" )))

'Set item 1 image
ListView1.Items (1).ImageIndex = ImageList1.Imag es.Count - 1

'Load from file
Dim s As String = Application.Sta rtupPath
Dim FileName As String = s.Substring(0, s.LastIndexOf(" \"c) + 1) &
"help2.ico"
ImageList1.Imag es.Add(New System.Drawing. Icon(FileName))

'Set item 2 image
ListView1.Items (2).ImageIndex = ImageList1.Imag es.Count - 1

End Sub

"Mick Doherty"
<EX***********@ AND.REMOVE.SQUA REBRACKETS.[mdaudi100#ntlwo rld.com]> wrote
in
message news:eK******** ******@tk2msftn gp13.phx.gbl...
How are you adding them?
In what way are they corrupted?

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"Carl Scarlett" <ab*@de.f.g> wrote in message
news:OF******** ******@TK2MSFTN GP12.phx.gbl...
> When I display icons in controls from an imagelist, the icons are
> "corrupted" if they are added from a resource (or a file) but are fine if > I
> add them using the designer at design time. I need to add the icons
> dynamically at runtime.
>
> How can you add the icons at runtime without corrupting the image?
>
>



Jul 21 '05 #4
Excellent. I've tested and it does resolve the problem.

Thanks Mick (and Matthew Hazlett).

"Mick Doherty"
<EX***********@ AND.REMOVE.SQUA REBRACKETS.[mdaudi100#ntlwo rld.com]> wrote in
message news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
The class at the following link will help you to extract a specific icon
from the Icon Resource.
http://www.codeproject.com/dotnet/MultiIcon.asp

Your code looks fine.

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"Carl Scarlett" <ab*@de.f.g> wrote in message
news:ew******** ******@TK2MSFTN GP10.phx.gbl...
Here's some code from my test project. I have an icon "help2.ico" that
contains all possible image sizes and formats. At design time, I add it
to
ImageList1. I also create ListView1 with 3 items in it, and add the icon to
the project as an embedded resource.

When you run the project, the last two items have corrupted icons. It
looks
like the icons are being resized from a larger icon size. How do I tell
it
to use the correct icon size from the icon?

Code follows:
--------------

Private Sub Form1_Load(ByVa l sender As Object, ByVal e As
System.EventArg s) Handles MyBase.Load

'Set image list
ListView1.Small ImageList = ImageList1

'Set item 0 image
ListView1.Items (0).ImageIndex = 0

'Load from resource
ImageList1.Imag es.Add(New
System.Drawing. Icon(System.Ref lection.Assembl y.GetExecutingA ssembly.GetMani f estResourceStre am(Me.GetType, "help2.ico" )))

'Set item 1 image
ListView1.Items (1).ImageIndex = ImageList1.Imag es.Count - 1

'Load from file
Dim s As String = Application.Sta rtupPath
Dim FileName As String = s.Substring(0, s.LastIndexOf(" \"c) + 1) & "help2.ico"
ImageList1.Imag es.Add(New System.Drawing. Icon(FileName))

'Set item 2 image
ListView1.Items (2).ImageIndex = ImageList1.Imag es.Count - 1

End Sub

"Mick Doherty"
<EX***********@ AND.REMOVE.SQUA REBRACKETS.[mdaudi100#ntlwo rld.com]> wrote
in
message news:eK******** ******@tk2msftn gp13.phx.gbl...
How are you adding them?
In what way are they corrupted?

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"Carl Scarlett" <ab*@de.f.g> wrote in message
news:OF******** ******@TK2MSFTN GP12.phx.gbl...
> When I display icons in controls from an imagelist, the icons are
> "corrupted" if they are added from a resource (or a file) but are
fine if
> I
> add them using the designer at design time. I need to add the icons
> dynamically at runtime.
>
> How can you add the icons at runtime without corrupting the image?
>
>



Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
49011
by: Ed Sutton | last post by:
I have been searching for how to get an icon resource and load it into an ImageList. I added an *.ico file to my project and set the files "Build Action" to "Embedded Resource". Is there any easy way to do this? This was easy to do in the old MFC. I can not find the answer in C#. I also need to embed an XML file into the resource and load it at run time. I am hoping once I figure out how to load an icon resource then loading an XML...
2
13199
by: Ed Sutton | last post by:
What's the trick to loading an icon from the embedded resources? Here are the steps I have taken: ------------------------- 1 - I added a file named "FOLDER01.ICO" to my solution project 2 - I changed "Build Action" for the file to "Embedded Resource" 3 - At the end of my constructor, I try to load an icon and it throws the following exception: Exception:
4
3731
by: Terry | last post by:
There are a number of things about using unmanaged resources in Windows Forms programming that is unclear to me. In C++, if you loaded an icon resource using "ExtractIcon()", the resource was valid until you called "DestroyIcon()". If you didn't, you had a leak. In C#, I need to load icons from unmanaged programs (.exe, dll) at run time and I want to make sure I'm not leaking resources, but it's not clear to me how to insure that I'm...
8
2281
by: Adrian | last post by:
How do I put an icon on a form using code? Thank you.
0
1494
by: Tony Lugg | last post by:
I have an application with a document management form. When users add documents to the form, I call the API function SHGetFileInfo to get the associated large and small icons for the file. These icons are added to two ImageList objects which are bound to a ListView control, and everything looks great. I am saving the icons to a SQL Server table by using Icon.Save to a stream and assigning the byte array to the field, then loading them...
3
2758
by: Tony Lugg | last post by:
I have an application with a document management form. When users add documents to the form, I call the API function SHGetFileInfo to get the associated large and small icons for the file. These icons are added to two ImageList objects which are bound to a ListView control, and everything looks great. I am saving the icons to a SQL Server table by using Icon.Save to a stream and assigning the byte array to the field, then loading them...
4
251
by: Carl Scarlett | last post by:
When I display icons in controls from an imagelist, the icons are "corrupted" if they are added from a resource (or a file) but are fine if I add them using the designer at design time. I need to add the icons dynamically at runtime. How can you add the icons at runtime without corrupting the image?
13
2078
by: Lou | last post by:
if I add a new item (Solution Items) to my project and its an icon(.ico), how can I reference that file when I am coding, Do I have to also add it to an image control, I don't want the file to be part of the .resx file. Its like VB6 when I could use load all my images from a resource file. Lou
4
6552
by: randy1200 | last post by:
I have a Windows application that previously had the company logo "MyCompany.ico" added to the upper left-most corner. The company has since issued a new version of "MyCompany.ico" that looks completely different. I overwrote the old ico file with the new ico file and re-ran the program. I still see the old logo at run-time. I right-clicked the project-> Add Existing Item-> added the new ico file. I set the build action to Embedded...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9561
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9381
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9332
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8252
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.