Link to home
Start Free TrialLog in
Avatar of gbmcneil
gbmcneil

asked on

Loading an icon into a Picturebox in VB 6.0

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?
Avatar of Lukasz Chmielewski
Lukasz Chmielewski
Flag of Poland image

Try to adjust number of colors in the picturebox properties to 32bpp
Avatar of gbmcneil
gbmcneil

ASKER

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.
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.

I meant setting pixel format for picturebox (maybe this is the issue with the image), see this thread - (C#)
https://www.experts-exchange.com/questions/21028351/Setting-the-Image-of-a-PictureBox.html?anchorAnswerId=11814076#a11814076
I am confused. What are you telling me to do?
Set the picturebox pixelformat value to mentioned in the thread and then assign the image programatically to it.
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.

Oops. I thought it was .NET Basic > 6
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.
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
ASKER CERTIFIED SOLUTION
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?

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.
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

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

https://www.experts-exchange.com/questions/26839104/Still-trying-to-load-a-48x48x32-icon-into-a-VB-NET-Picturebox.html?cid=239&anchorAnswerId=34954477#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?.
eql1044 -

Maybe we should author a book on this subject.
Please see your other question where It's more clear about what I'm trying to explain here without all the run around.
Agreed.
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

Nope. You can set the SizeMode property.
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
PictureBox1.Image = Me.Icon.ToBitmap

Open in new window

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.
Thanks for all your help.