Converting a Picture to an Icon

Converting a Picture to an Icon

Post by er12 » Mon, 21 Apr 2003 17:39:32



I am trying to convert a picture to an Icon - so that I can have a paste
function to change the application icon.

I originally wrote my program in VB3 and used the code below (Originally
fron the Icon Editor in VB3)
(The new picture is in the processbox and there is an existing icon in the
active control ( The getbitmapbits simply replaces the bitmap).

    lp = GlobalLock(Screen.ActiveForm.ActiveControl.Picture)
    re = GetBitmapBits(Screen.ActiveForm.processbox.Image, 512, lp + 12 +
128)
    res = GlobalUnlock(Screen.ActiveForm.ActiveControl.Picture)

the activecontrol and the processbox are both picturebox controls

Now in VB6 the value returened by the GlobalLock function is 0 - so the code
fails

Does anyone know why? Or is there a better way to create the icon in the
picturebox?

Any help would be apreciated.

Eric Raymond

 
 
 

Converting a Picture to an Icon

Post by Mike D Sutto » Mon, 21 Apr 2003 18:21:38


Quote:> I am trying to convert a picture to an Icon - so that I can have a paste
> function to change the application icon.

> I originally wrote my program in VB3 and used the code below (Originally
> fron the Icon Editor in VB3)
> (The new picture is in the processbox and there is an existing icon in the
> active control ( The getbitmapbits simply replaces the bitmap).

>     lp = GlobalLock(Screen.ActiveForm.ActiveControl.Picture)
>     re = GetBitmapBits(Screen.ActiveForm.processbox.Image, 512, lp + 12 +
> 128)
>     res = GlobalUnlock(Screen.ActiveForm.ActiveControl.Picture)

> the activecontrol and the processbox are both picturebox controls

> Now in VB6 the value returened by the GlobalLock function is 0 - so the
> code
> fails

> Does anyone know why? Or is there a better way to create the icon in the
> picturebox?

GetBitmapBits() extracts the bitmap data from an API Bitmap object and dumps
it at a memory location.  In your case you're reading from one API Bitmap
object and writing into an explicit offset within a second API Bitmap object
(Presumably to avoid overwriting the palette or something, I can't see much
of a correlation between the offsets and the internal structure of a Bitmap
though..)
Try this version instead and see if that makes any difference:

'***
Dim RetBytes() As Byte

Const DataSize = 512

ReDim RetBytes(DataSize - 1) As Byte

With Screen.ActiveForm
    Call GetBitmapBits(.processbox.Image.Handle, DataSize, RetBytes(0))
    Call SetBitmapBits(.ActiveControl.Picture.Handle, DataSize, RetBytes(0))
End With

Erase RetBytes
'***

Here's the SetBitmapBits() declare if you need it:

'***
Private Declare Function SetBitmapBits Lib "gdi32" Alias "SetBitmapBits" _
    (ByVal hBitmap As Long, ByVal dwCount As Long, ByRef lpBits As Any) As
Long
'***

Hope this helps,

    Mike

 -- EDais --

 - Microsoft Visual Basic MVP -
WWW: Http://www20.brinkster.com/EDais/



 
 
 

1. Converting a picture to an Icon

Mike,

Thank you for taking the time to offer me some suggestions about converting
a picture to an icon.

The first suggestion was too simple to work.

I had for a while thought about creating the icon completely in memory and
then allocating it. that seemed rather daunting, so thank you for showing me
an example of how to do it.

Unfortunately - the code creates an icon, and saves it to fil OK BUT

- I cannot use the resultant icon
- the part in the code which  draws the icon
(        Call DrawIconEx(Picture2.hDC(), 10       , 10  , hIcon, 0, 0, 0, 0,
DI_NORMAL)
        Call DrawIconEx(Picture2.hDC(), 10       , IconHeight + 20 , hIcon,
0, 0, 0, 0, DI_IMAGE)
        Call DrawIconEx(Picture2.hDC(), IconWidth + 20, IconHeight + 20 ,
hIcon, 0, 0, 0, 0, DI_MASK)
)

doesn't draw anything.
- the icon fili is recognised as an icon - in that I can allocate it to
something on my desk top, but VB will not load it - "invalid format"
also I can't open it say in Corel - "Invalid header".

What is happening - can there be a reason that VB doesn't recognise the
structure of the icon file? I tried a couple of different screen settings to
see if that helped - but it didn't.

If you can't help further, thank you for your rime anyway,

Eric Raymond

2. printing image to HP Laser

3. Need help with POV under Win95 - wont display

4. convert icon to picture

5. World Building in 3d

6. Converting Large Icons to Small Icons

7. Problems with Clipping Perspective Coords To Screen

8. Problem converting Mac Icons to Windows Icon

9. Sub Color2Gray(Icon as Picture) as Picture ???

10. Convert Picture in Picture with numerated Points

11. Convert Handle of picture (hBmp) in Address of data of picture

12. ICONS - CREATING ICON.DLL or ICON.EXE FROM *.ICO