Option Strict Question

OSVBNET 1,386 Reputation points
2022-04-17T23:06:01.147+00:00

Hey,
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 ^_^

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

3 answers

Sort by: Most helpful
  1. Viorel 114.7K Reputation points
    2022-04-18T02:27:36.733+00:00

    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
    varImg.Save("D:\img.bmp")

    0 comments No comments

  2. Leon Stanley 96 Reputation points
    2022-04-18T06:36:11.497+00:00

    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?
    Simply
    Dim bmp as Bitmap
    bmp = New Bitmap(My.Resources.bmp.bmp) ' does not work ?

    0 comments No comments

  3. OSVBNET 1,386 Reputation points
    2022-04-18T07:15:27.9+00:00

    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