Option Strict and .Save with an Image

G

Guest

The following code works when Option Strict is off:
Dim thumbSize As New Size
thumbSize = CType(NewthumbSize(g.Width, g.Height, 100), System.Drawing.Size)
Dim imgThumbOutput As New Bitmap(g, thumbSize.Width, thumbSize.Height)
imgThumbOutput.Save(Server.MapPath(ThumbnailPath), thisFormat)

When I turn on Option Strict, I receive the following compile error:
Overload resolution failed because no accessible 'Save' can be called with
these arguments:
'Public Sub Save(stream As System.IO.Stream, format As
System.Drawing.Imaging.ImageFormat)': Value of type 'String' cannot be
converted to 'System.IO.Stream'.
'Public Sub Save(stream As System.IO.Stream, format As
System.Drawing.Imaging.ImageFormat)': Option Strict On disallows implicit
conversions from 'System.Object' to 'System.Drawing.Imaging.ImageFormat'.
'Public Sub Save(filename As String, format As
System.Drawing.Imaging.ImageFormat)': Option Strict On disallows implicit
conversions from 'System.Object' to 'System.Drawing.Imaging.ImageFormat'.

Tried converting the path:
imgThumbOutput.Save(CType(Server.MapPath(ThumbnailPath), System.IO.Stream),
thisFormat)

but it errored because you can't convert a string into a System.IO.Stream.

Is there a way of rewriting this line to work with Option Strict? Thanks.
 
A

Adam Goossens

Hi,

I think the problem lies not in your path, but your format. If the
"thisFormat" variable is of type Object you need to cast it to the
System.Drawing.Imaging.ImageFormat type first:
 
C

Cor Ligthert

JMHMaine.

The solution in this is mostly to break your instructions (for testing) in
smaller parts.

Than often you see yourself the best where the problem is.

Just an idea.

Cor
 
H

Herfried K. Wagner [MVP]

jmhmaine said:
When I turn on Option Strict, I receive the following compile error:
Overload resolution failed because no accessible 'Save' can be called with
these arguments:
'Public Sub Save(stream As System.IO.Stream, format As
System.Drawing.Imaging.ImageFormat)': Value of type 'String' cannot be
converted to 'System.IO.Stream'.
'Public Sub Save(stream As System.IO.Stream, format As
System.Drawing.Imaging.ImageFormat)': Option Strict On disallows implicit
conversions from 'System.Object' to 'System.Drawing.Imaging.ImageFormat'.
'Public Sub Save(filename As String, format As
System.Drawing.Imaging.ImageFormat)': Option Strict On disallows implicit
conversions from 'System.Object' to 'System.Drawing.Imaging.ImageFormat'.

\\\
imgThumbOutput.Save(Server.MapPath(ThumbnailPath), f)
///

should work if 'f' is of type 'ImageFomat'.
 
G

Guest

Thanks, you were right, it was the second argument "thisFormat", not the
first one that was causing the error.

The lines above, which I forgot to include:
Dim g As System.Drawing.Image =
System.Drawing.Image.FromFile(Server.MapPath(Path))
Dim thisFormat As Object = g.RawFormat

I changed the second line to:
Dim thisFormat As System.Drawing.Imaging.ImageFormat = g.RawFormat

and it worked! Thanks.

Josh.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

option strict 5
Option strict 4
Error when 'option strict' 2
Option Strict On 22
Conversion issue with option strict 5
Option Strict On and Ubound 4
How to read image/blob field with option strict on 4
Strict On 3

Top