VB code help

  • Thread starter Thread starter K
  • Start date Start date
K

K

Hi all, I am creating an application on Visual Basic 2008 to convert
any image into icon. I have written code (see below) which works
fine. It converts any image to icon in specified size. But the
problem is that when I try to use that icon I get error message
saying "invalid image property" or "invalid image" etc. Please can
any friend tell me that what amendments i need in my code below to
achive my objective. help in shape of code will be much appriciated.


Dim imageFinal As System.Drawing.Image = Nothing
Dim g As System.Drawing.Graphics = Nothing
imageFinal = New System.Drawing.Bitmap(64, 64)
g = System.Drawing.Graphics.FromImage(imageFinal)
Dim destRect As New System.Drawing.Rectangle(0, 0, 64, 64)
Dim srcRect As New System.Drawing.Rectangle(0, 0,
PictureBox1.Image.Width, PictureBox1.Image.Height)
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawImage(PictureBox1.Image, destRect, srcRect,
System.Drawing.GraphicsUnit.Pixel)
imageFinal.Save(RichTextBox2.Text & "\" & RichTextBox3.Text & ".ico",
System.Drawing.Imaging.ImageFormat.Icon)
MsgBox("Image has been converted", MsgBoxStyle.Information, "Done!")
 
K said:
Hi all, I am creating an application on Visual Basic 2008 to convert
any image into icon. I have written code (see below) which works
fine. It converts any image to icon in specified size. But the
problem is that when I try to use that icon

What does "use" mean? Which line fails? The code below works if
an image has been assigned to picturebox1.image.

....Well, I guess the problem is described here:
http://support.microsoft.com/default.aspx?scid=kb;en-us;q316563

So I suggest to explicitly create an Icon object by calling
- System.Drawing.Bitmap.GetHicon
- System.Drawing.Icon.FromHandle
- System.Drawing.Icon.Save

Optionally create the Icon from an image returned from the
original image's GetThumbnailImage method.
 
Armin said:
What does "use" mean? Which line fails? The code below works if
an image has been assigned to picturebox1.image.

Sorry, you already wrote that it works fine for you, too.
Just found my glasses....
 
- System.Drawing.Bitmap.GetHicon
- System.Drawing.Icon.FromHandle
- System.Drawing.Icon.Save

Hi Armin. Thanks for replying. I have used above code lines which
you mentioned in your answer in my another code (see below) as well,
but I get two problems. One that I loose image actual colours and
graphics and two the code below don't genrate or save 16 x 16 icons
nicely as I get balck background. I am simply trying to create icons
which should look exactly the same of image from which its been
created but in 16 x 16 , 32 x 32 , 24 x 24 , 48 x 48 and 64 x 64
sizes. Any suggestions ?

Dim img As New Bitmap(PictureBox1.Image, 32, 32)
Dim g As Graphics = Graphics.FromImage(img)
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawImage(img, 0, 0)
Dim Hicon As IntPtr = img.GetHicon
Dim newicon As Icon = System.Drawing.Icon.FromHandle(Hicon)
Dim fpth As New IO.FileStream(RichTextBox2.Text & "\" &
RichTextBox3.Text & ".ico", IO.FileMode.Create)
newicon.Save(fpth)
newicon.Dispose()
MsgBox("Image has been converted", MsgBoxStyle.Information, "Done!")
 
Back
Top