Solved

Loading an icon into a Picturebox in VB 6.0

Posted on 2011-02-21
27
1,069 Views
Last Modified: 2012-05-11
Hello Experts -

I am trying to load a 48 x 48 pixel icon into a Picturebox control in VB 6.0. What I'm getting is very grainy replication of what the icon looks like when it is displayed on the desktop.

Now, I haven't changed any other value(s) in the Property Sheet for this Picturebox, and I haven't tried any different controls.

Any ideas how I could preserve the quality?
0
Comment
Question by:gbmcneil
  • 14
  • 9
  • 4
27 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34947097
Try to adjust number of colors in the picturebox properties to 32bpp
0
 

Author Comment

by:gbmcneil
ID: 34947220
Thanks for responding.

I don't see any property that indicates the number of "bits per pixel" (is that what you are referring to?).

I could change the number of colors in a program like Paint Shop Pro 6.0, but I think that would make things even grainer.
0
 

Author Comment

by:gbmcneil
ID: 34947288
Roads Roads -

When I bring the icon into ArtIcons I can see the graphic in different pixel renditions. I see one called "48 x 48 x 32" and that has the besrt resolution. At this rendition it appears a little bit smaller than the same icon when it is placed on my desktop and a little less vibrant.

I found the icon in shell.dll in the System32 directory of a new Sony 64-bit computer. Maybe the icon is better than even the icon editors can detect.

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34947289
I meant setting pixel format for picturebox (maybe this is the issue with the image), see this thread - (C#)
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21028351.html#a11814076
0
 

Author Comment

by:gbmcneil
ID: 34947414
I am confused. What are you telling me to do?
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34947446
Set the picturebox pixelformat value to mentioned in the thread and then assign the image programatically to it.
0
 

Author Comment

by:gbmcneil
ID: 34947530
If you are telling me to use this line of code -

PixelFormat pf = PixelFormat.Format32bppRgb

or more correctly -              pf = PixelFormat.Format32bppRgb

this is VB.NET talk. I'm in VB 6.0.

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34947555
Oops. I thought it was .NET Basic > 6
0
 

Author Comment

by:gbmcneil
ID: 34947698
I tried to bring the ico file into Paint Shop Pro 6.0.

PSP could not read the file. Apparently 48 x 48 x 32 is pretty advanced stuff. That's why my desktop on the new 64-bit Sony laptop looks so great.

So, I guess if I'm going to do this thing, I'll have to do it in VB.NET. Give me a little time to install Visual Studio and get back to where I was.
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 34948053
Have you tried using LoadImage() and DrawIcon() onto the PictureBox HDC surface?

LoadImage
http://msdn.microsoft.com/en-us/library/ms648045(v=vs.85).aspx

DrawIconEx
http://msdn.microsoft.com/en-us/library/ms648065(v=vs.85).aspx
0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 250 total points
ID: 34948298
Try using something like this
Option Explicit

Private Enum ImageType
IMAGE_BITMAP = 0 'Loads a bitmap.
IMAGE_CURSOR = 2 'Loads a cursor.
IMAGE_ICON = 1 'Loads an icon.
End Enum

Private Const LR_LOADFROMFILE As Long = &H10&
Private Const LR_DEFAULTSIZE As Long = &H40&

Private Const DI_MASK As Long = &H1&
Private Const DI_IMAGE As Long = &H2&
Private Const DI_COMPAT As Long = &H4&
Private Const DI_NORMAL As Long = DI_MASK Or DI_IMAGE
Private Const DI_DEFAULTSIZE As Long = &H8&

Private Declare Function LoadImageW Lib "User32.dll" ( _
  ByVal hinst As Long, _
  ByVal lpszName As Long, _
  ByVal uType As Long, _
  ByVal cxDesired As Long, _
  ByVal cyDesired As Long, _
  ByVal fuLoad As Long) As Long

Private Declare Function DrawIconEx Lib "User32.dll" ( _
  ByVal hDc As Long, _
  ByVal xLeft As Long, _
  ByVal yTop As Long, _
  ByVal hIcon As Long, _
  ByVal cxWidth As Long, _
  ByVal cyWidth As Long, _
  ByVal istepIfAniCur As Long, _
  ByVal hbrFlickerFreeDraw As Long, _
  ByVal diFlags As Long) As Long
 
Private Declare Function DrawIcon Lib "User32.dll" ( _
  ByVal hDc As Long, _
  ByVal X As Long, _
  ByVal Y As Long, _
  ByVal hIcon As Long) As Long
  
Private Declare Function DestroyIcon Lib "User32.dll" ( _
  ByVal hIcon As Long) As Long

Public Sub DrawIconToPictureBox(ByVal pic As PictureBox, ByVal iconPath As String, ByVal cx As Long, ByVal cy As Long)
  Dim hIcon As Long
  pic.Cls '// clear previous icon
  hIcon = LoadImageW(0, StrPtr(iconPath), IMAGE_ICON, cx, cy, LR_LOADFROMFILE)
  DrawIconEx Picture1.hDc, 0, 0, hIcon, cx, cy, 0, 0, DI_NORMAL Or DI_COMPAT
  DestroyIcon hIcon
  pic.Refresh '// refresh
End Sub

Private Sub Form_Load()
  '// TODO: Set PictureBox AutoRedraw = TRUE
  Picture1.AutoRedraw = True
  DrawIconToPictureBox Picture1, "c:\windows\system32\PerfCenterCpl.ico", 128, 128
End Sub

Open in new window

0
 

Author Comment

by:gbmcneil
ID: 34948782
Okay, eqli1044.  Thanks for getting involved.

I'm a newbie to VB.NET so give me a little leeway here.

When I put your code in the Declarations section of my form, VB.NET doesn't like just about every line. Do I have to put the Windows functions in a module?  Or, can I put all the code in the Declarations section of my form?

0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 34948796
My bad... I thought you said above you were using VB6? The example is for VB6 ;)... I'm just curious why you get that behavior and want to see if the above might help but I'm not sure what type of icon your refering about though.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:gbmcneil
ID: 34949012
Hello eql1044 -

Five sections ago, I announced that I was moving the project to VB.NET in order to find a solution to my problem.

Then I took your code and inserted it into my VB.NET project as best I could. I put the functions into a module and the remainder of the code in a form (with my PictureBox mounted on it).

Now, here is the current status of the code in the VB.NET form.

The first thing it doesn't like is the statement  pic.Cls. I don't think Cls exists as a method in VB.NET.

The second thing it doesn't like is StrPtr. Apparently, that function doesn't exist in VB.NET either.

Any thoughts?
Option Explicit On

Public Class SXBFRMHST

    Private Sub DrawIconToPictureBox(ByVal pic As PictureBox, ByVal iconPath As String, ByVal cx As Long, ByVal cy As Long)
        Dim hIcon As Long
        pic.Cls() '// clear previous icon
        hIcon = LoadImageW(0, StrPtr(iconPath), ImageType.IMAGE_ICON, cx, cy, LR_LOADFROMFILE)
        DrawIconEx(pic.hDc, 0, 0, hIcon, cx, cy, 0, 0, DI_NORMAL Or DI_COMPAT)
        DestroyIcon(hIcon)
        pic.Refresh() '// refresh
    End Sub


    Private Sub SXFRMHIST_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Call DrawIconToPictureBox(PictureBox1, "C:\Projects\VB.Net 2008\Progman 1.0\Progman\Icons\Feb15History.ico", 48, 48)


    End Sub

End Class

Open in new window

0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 34949025
If your moving to VB.NET then the example won't help you as it was for VB6. Unless your using VB6 then forget about using this code in .NET
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 34957175
How come you don't try the example in VB6... and compare it to the .NET example your using .. Let me know the difference ( The VB6 example prob work as intended)
0
 

Author Comment

by:gbmcneil
ID: 34961540
Hello eql1044 -

I learned a valuable lesson here beyond dealing with icons and Pictureboxs.

It's time to move on to VB.NET.

It was not worth it struggling with a platform that has no future. I had other problems at the same time, and it made no sense investing in OCXs and the like. If I knew how to close this question, I would have - or will.

It just so happens I re-opened the question as a VB.NET problem and had a solution within 2 hours.

gbmcneil
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 34962187
I understand but a single icon can have multiple images within 32, 48, 64, 128, 256.. Your saying in your question the icon becomes blurry in .NET which is why I wanted you to test the code to see if you get smooth resolution using the VB6 example. If you get the blurry image using the VB6 code then your icon doesn't have higher resoultions. You may get a blurry image in .NET when stretching but not using the VB6 example which loads the correct resolution from the file.

If you take this icon in .NET ( c:\windows\system32\PerfCenterCpl.ico ) and try to draw it @ 128, 256 what do you see? Blurry?

The same icon in VB6 example doesn't become blurry.

I'm not trying to tell you to use VB6, only want you to test the code to see if it's an issue with .NET ;)
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 34962235
The VB6 example drawing the icon @ 256. You see this isn't blurry.. Do you get this same resolution in .NET? Thats all I'm trying to figure out bud ;)
drawicon256.bmp
0
 

Author Comment

by:gbmcneil
ID: 34962708
Hello again eql1044 -

I'm sorry Mr. Expert, but I'm not an expert. I generally take the path of least resistance or I'd end up spending a year on the same project.

Here is what I can tell you. I resubmitted the question to the Experts here:

http://www.experts-exchange.com/Microsoft/Development/Q_26839104.html?cid=239#a34954477

A fellow by the name of quizwedge suggested:

http://stackoverflow.com/questions/914323/display-icon-on-form-in-vb-net

Although the code worked, the icon was displayed maybe 15% smaller in the Picturebox than it was displayed on my desktop. It was also less viibrant. When I tried to stretch the 48x48 icon to 64x64 to make it larger, it looked worse.

So, taking the path of least resistance, I purchased some business-style icons at a resolution of 64x64 from Glyfx in Australia. They cost me about $60. These seem to work nicely because they're at a higher resolution - insuring size and vibrancy (is that a word?).

But, there is no way I could take a 64x64 icon and enlarge it to the full width of a dialogbox. If you would like me to pursue your Windows API approach to loading an icon into a Picturebox in VB.NET, I'd be game. But, the problem is that the Picturebox control in VB.NET isn't the same as the Picturebox control in VB 6.0. When it comes time to retrieve the hDC of the Picturebox in VB.NET, I don't think it's provided.

I suspect the stock Microsoft controls in VB.NET 2008 leave a lot to be desired.

Your thoughts?.
0
 

Author Comment

by:gbmcneil
ID: 34962741
eql1044 -

Maybe we should author a book on this subject.
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 34962743
Please see your other question where It's more clear about what I'm trying to explain here without all the run around.
0
 

Author Comment

by:gbmcneil
ID: 34962763
Agreed.
0
 

Author Comment

by:gbmcneil
ID: 34966246
eql1044 -

I now find when I use this code, that the image is shifted to the far left in the Picturebox control.

I am not doing anything special in the Property Sheet or programmatically.

Should I try another Picturebox control?
'Code for loading the icon into a PictureBox 
        Dim theIcon As Icon = LoadIconFromFile("C:\Projects\VB.Net 2008\Progman 1.0\Icons\Feb15History.ico")
        PictureBox1.Image = theIcon.ToBitmap()
        theIcon.Dispose()

Open in new window

0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 34966276
Nope. You can set the SizeMode property.
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
PictureBox1.Image = Me.Icon.ToBitmap

Open in new window

0
 

Author Comment

by:gbmcneil
ID: 34967617
The darn image in the PictureBox control is left justified.

I've made sure that in the original icon file the image is centered. I've loaded it in the Picturebox as you have suggested a la "CenterImage", and the Property Sheet has been set so that the value of SizeMode is "CenterImage".

But, to no avail. The PictureBox shifts the image to the left.
0
 

Author Closing Comment

by:gbmcneil
ID: 34988129
Thanks for all your help.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now