Option Strict Question

OSVBNET 1,386 Reputation points

I'm using this code to extract my bmp resource:

Dim varRes As Object = My.Resources.bmp.bmp
Dim varImg As Drawing.Bitmap = DirectCast(varRes, Drawing.Image)
varImg.Save("D:\img.bmp", System.Drawing.Imaging.ImageFormat.Bmp)

No idea if doing it the best way but however turning on the Option Strict:
Option Strict On disallows implicit conversions from 'System.Drawing.Image' to 'System.Drawing.Bitmap'
How to fix it without using legacy CType etc?

Thanks ^_^

An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,629 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 113.8K Reputation points

    Try this:

    Dim varImg As Drawing.Image = DirectCast(varRes, Drawing.Image)

    or this:

    Dim varImg As Drawing.Bitmap = DirectCast(varRes, Drawing.Bitmap)

    or this:

    Dim varImg = DirectCast(varRes, Drawing.Image)

    Maybe you can also simplify it:

    Dim varImg = My.Resources.bmp.bmp

    0 comments No comments

  2. Leon Stanley 96 Reputation points

    I always code with Strict On and Infer Off
    Simply use
    Dim bmp as Bitmap
    bmp = New Bitmap("c:\someDirectory\someImage.png") ' either .bmp or .jpg or whatever
    then to save a bitmap
    bmp.Save("c:\someDirectory\someImage.png", Imaging.ImageFormat.Png) ' or .Bmp or .Jpg or whatever

    Are you going through that gymnastic to get access to a My.Resources image?
    Dim bmp as Bitmap
    bmp = New Bitmap(My.Resources.bmp.bmp) ' does not work ?

    0 comments No comments

  3. OSVBNET 1,386 Reputation points

    In a single line call:

    My.Resources.bmp.bmp.Save("D:\img.bmp", System.Drawing.Imaging.ImageFormat.Bmp)

    Didn't know it works that simple?

    0 comments No comments