472,102 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,102 software developers and data experts.

Displaying image

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
Nov 20 '05 #1
18 1757
PS: picImage is a PictureBox.

"John" <jo**@nospam.infovis.co.uk> wrote in message
news:Ox**************@TK2MSFTNGP09.phx.gbl...
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

Nov 20 '05 #2
"John" <jo**@nospam.infovis.co.uk> wrote in news:OxEaex3kDHA.1284
@TK2MSFTNGP09.phx.gbl:
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
Nov 20 '05 #3
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
Nov 20 '05 #4
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
Nov 20 '05 #5
"Fergus Cooney" <fi******@tesco.net> wrote in
news:u7**************@TK2MSFTNGP09.phx.gbl:
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
Nov 20 '05 #6
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
Nov 20 '05 #7
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
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:eh**************@tk2msftngp13.phx.gbl...
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

Nov 20 '05 #8
* "John" <jo**@nospam.infovis.co.uk> scripsit:
PS: picImage is a PictureBox.


Problem solved?

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #9
* "John" <jo**@nospam.infovis.co.uk> scripsit:
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(...)'

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #10
* "John" <jo**@nospam.infovis.co.uk> scripsit:
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.HighQualityBic ubic

' 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")
..
..
..
///

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #11
* Michael Bray <mb*******************@SkPiAlMl.ctiusa.com> scripsit:
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.

;-)

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #12
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
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:um**************@tk2msftngp13.phx.gbl...
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

Nov 20 '05 #13
* "Tom Spink" <th**********@ntlworld.com> scripsit:
Michael: This means war. You'd better duck, or Me and Herfried will hunt you
down.


VB.NET rules.

;-)))

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #14
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.Ge tManifestResourceStream(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;
}
}

--
Justin Weinberg

Designing a PrintDocument? Drawing to forms?
Check out GDI+ Architect at www.mrgsoft.com
"John" <jo**@nospam.infovis.co.uk> wrote in message
news:Ox**************@TK2MSFTNGP09.phx.gbl...
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

Nov 20 '05 #15
Definitely. Thanks. Now I am trying to zoom/scale the image, when it is too
large to fit in the control/form.

Thanks

Regards
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bm************@ID-208219.news.uni-berlin.de...
* "John" <jo**@nospam.infovis.co.uk> scripsit:
PS: picImage is a PictureBox.


Problem solved?

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Nov 20 '05 #16
Thanks. I will try this.

Regards
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bm************@ID-208219.news.uni-berlin.de...
* "John" <jo**@nospam.infovis.co.uk> scripsit:
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.HighQualityBic ubic

' 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")
.
.
.
///

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Nov 20 '05 #17
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" <jo**@nospam.infovis.co.uk> wrote in message
news:OG**************@tk2msftngp13.phx.gbl...
Definitely. Thanks. Now I am trying to zoom/scale the image, when it is too large to fit in the control/form.

Thanks

Regards
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bm************@ID-208219.news.uni-berlin.de...
* "John" <jo**@nospam.infovis.co.uk> scripsit:
PS: picImage is a PictureBox.


Problem solved?

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>


Nov 20 '05 #18
Howdy Bob,

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

Regards,
Fergus
Nov 20 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Gregory | last post: by
5 posts views Thread by ljuljacka | last post: by
4 posts views Thread by redpears007 | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.