Displaying image

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I am trying to display an image in a dialog using the following code;

Dim frm As New frmImage
Dim z As System.Drawing.Bitmap

z.FromFile("c:\temp\image.gif")
frm.picImage.Image = z
frm.ShowDialog()
frm.Dispose()

Trouble is that no image appears in the dialog. What am I doing wrong?

Thanks

Regards
 
John said:
I am trying to display an image in a dialog using the following code;

Dim frm As New frmImage
Dim z As System.Drawing.Bitmap

z.FromFile("c:\temp\image.gif")
frm.picImage.Image = z
frm.ShowDialog()
frm.Dispose()

Trouble is that no image appears in the dialog. What am I doing wrong?

I presume from the fact that you aren't indicating that the compiler is
failing that you really mean

z = Image.FromFile("c:\temp\image.gif");

but there is another error. I think you are having a problem with your
backslashes in the filename... In general, you should always check a
variable before you use it. I would put money of the supposition that 'z'
is null after the call to FromFile(). To FIX the problem replace the line
with either:

z = Image.FromFile(@"c:\temp\image.gif");

(the @ tells the compiler not to interpret the '\' characters), OR

z = Image.FromFile("c:\\temp\\image.gif");

-mbray
 
Evening John,

Put a breakpoint on the Image = z line and check the value of z. You'll
find that it's Nothing.

z.FromFile() is calling a <shared> method. It's the same as calling
Bitmap.FromFile(..). In other words it doesn't (can't) change z.

What you need is either:
Dim z As System.Drawing.Bitmap
z = Bitmap.FromFile("c:\temp\image.gif")
or simply
Dim z As New System.Drawing.Bitmap ("c:\temp\image.gif")

Regards,
Fergus
 
Howdy Michael,

It's in VB so the syntax is fine, but it's true that the result should be
assigned to z.

Regards,
Fergus
 
It's in VB so the syntax is fine, but it's true that the result
should be
assigned to z.

Yeah I realized that and knocked on my head a few times after I saw your
original reply. This is why I think we should just get rid of VB
altogether. *ducks* (just kidding, of course.)

-mbray
 
Hi Michael,

ROFL. Many a serious word spoken in jest.

Presumably you're from a framework group, so welcome to this glimpse of
languages.vb. ;-)

Regards,
Fergus
 
Thanks. That worked.

Anyway to zoom the image if it is too big to fit in picturebox's current
size? Ideally I would like a zoom level that a user can set, otherwise
whatever is possible.

Thanks

Regards
 
* "John said:
I am trying to display an image in a dialog using the following code;

Dim frm As New frmImage
Dim z As System.Drawing.Bitmap

z.FromFile("c:\temp\image.gif")

Use this instead: 'z = Image.FromFile(...)'
 
* "John said:
Anyway to zoom the image if it is too big to fit in picturebox's current
size? Ideally I would like a zoom level that a user can set, otherwise
whatever is possible.

\\\
Dim bmp As New Bitmap( _
"C:\Lagoon1024x768.bmp" _
)
Dim bmp2 As New Bitmap( _
bmp.Width * 0.1, _
bmp.Height * 0.1, _
Imaging.PixelFormat.Format24bppRgb _
)
Dim g As Graphics = Graphics.FromImage(bmp2)

' Select/change interpolation mode here.
g.InterpolationMode = _
Drawing.Drawing2D.InterpolationMode.HighQualityBicubic

' Draw image using specified interpolation mode.
g.DrawImage(bmp, 0, 0, bmp2.Width, bmp2.Height)
g.Dispose()

Me.PictureBox1.Image = bmp2
bmp2.Save("C:\foo.bmp")
..
..
..
///
 
* Michael Bray said:
z = Image.FromFile("c:\temp\image.gif");

but there is another error. I think you are having a problem with your
backslashes in the filename... In general, you should always check a
variable before you use it. I would put money of the supposition that 'z'
is null after the call to FromFile(). To FIX the problem replace the line
with either:

z = Image.FromFile(@"c:\temp\image.gif");

Not in VB.NET... We don't need the '@' prefix.

;-)
 
Michael: This means war. You'd better duck, or Me and Herfried will hunt you
down.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
These functions may be useful if you're still having trouble.

'VB.NET

Private Function ImageFromAbsolutePath(ByVal absolutePath As String) As
System.Drawing.Bitmap
Dim img As System.Drawing.Bitmap = New
System.Drawing.Bitmap(absolutePath)
Return img
End Function

Private Function ImageFromRelativePath(ByVal relativePath As String) As
System.Drawing.Bitmap
Dim executingPath As String =
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssem
bly.Location)
Dim img As System.Drawing.Bitmap = New
System.Drawing.Bitmap(String.Concat(executingPath, "/", relativePath))
Return img
End Function

Private Function ImageFromEmbedded(ByVal embeddedPath As String) As
System.Drawing.Bitmap
Dim imageStream As System.IO.Stream =
System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(em
beddedPath)
Dim img As System.Drawing.Bitmap = New
System.Drawing.Bitmap(imageStream)
Return img
End Function


//C#
private System.Drawing.Bitmap ImageFromAbsolutePath(string absolutePath)
{
System.Drawing.Bitmap img = new System.Drawing.Bitmap(absolutePath);
return img;
}

private System.Drawing.Bitmap ImageFromRelativePath(string relativePath)
{
string executingPath =
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssem
bly().Location);
System.Drawing.Bitmap img = new
System.Drawing.Bitmap(string.Concat(executingPath, "/", relativePath));
return img;
}

private System.Drawing.Bitmap ImageFromEmbedded(string embeddedPath) {
System.IO.Stream imageStream =
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(
embeddedPath);
System.Drawing.Bitmap img = new System.Drawing.Bitmap(imageStream);
return img;
}
}
 
Definitely. Thanks. Now I am trying to zoom/scale the image, when it is too
large to fit in the control/form.

Thanks

Regards
 
Thanks. I will try this.

Regards


Herfried K. Wagner said:
\\\
Dim bmp As New Bitmap( _
"C:\Lagoon1024x768.bmp" _
)
Dim bmp2 As New Bitmap( _
bmp.Width * 0.1, _
bmp.Height * 0.1, _
Imaging.PixelFormat.Format24bppRgb _
)
Dim g As Graphics = Graphics.FromImage(bmp2)

' Select/change interpolation mode here.
g.InterpolationMode = _
Drawing.Drawing2D.InterpolationMode.HighQualityBicubic

' Draw image using specified interpolation mode.
g.DrawImage(bmp, 0, 0, bmp2.Width, bmp2.Height)
g.Dispose()

Me.PictureBox1.Image = bmp2
bmp2.Save("C:\foo.bmp")
.
.
.
///
 
The GDI+ FAQ has an article on displaying images in a given rectangle while
maintaining the aspect ratio of the original.

--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Blog http://bobpowelldotnet.blogspot.com

John said:
Definitely. Thanks. Now I am trying to zoom/scale the image, when it is too
large to fit in the control/form.

Thanks

Regards
 
Howdy Bob,

I've been a-browsing over the last few days- nice site you've got - lots
of goodies. ;-)

Regards,
Fergus
 
Back
Top