loading an image?

raghuism

Member
Joined
Mar 18, 2009
Messages
8
Programming Experience
Beginner
Hi,
Sorry if it is a dumb question :) . Well, i have a problem in loading an image from a file in vb.net. I have the following code in the class of my project.
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

public class ImageDisplay

public Shared Sub Main
Application.Run(New FrmDisplayLogo)
End Sub
End class
Public Class FrmDisplayLogo
Inherits System.Windows.Forms.Form



Private Sub FrmDisplayLogo_Load(ByVal sender As _
System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load

Dim width As Integer = 200
Dim height As Integer = 200

Dim mGraphicsObject As Graphics

mGraphicsObject = Me.CreateGraphics

mGraphicsObject.DrawImage(Image.FromFile("YourImage.bmp"), 5, 5, width, height)

End Sub
End Class

So, now I've exactly used this code to load an image from a path specified. Still, i seem to get a blank form when i run. im quite sure about the path and the existence of the file. Also, the exact piece of code is also available at:
Draw an image : Draw Image2D GraphicsVB.Net Tutorial
So, now do i have to insert a picture box to se the loaded image. Its like.. ive used the graphics thing to draw the image. so i reckoned that picture box was unnecessary.
How do i solve the problem now? Thanks a ton in advance.
 
BAD tutorial!! Never use a Graphics instance returned from Me.CreateGraphics to draw with. If you intend to draw dynamically use the Paint event, it provides e.Graphics that you can draw with. Else use a PictureBox to display image.
 
Well, How do i redraw an image i.e. in case im drawing a chess piece and want to show the moved position. For that, how should im able to draw the image onto a new positon. But how do i delete the previous location image in vb.net . Or, is there a chance that i could use any move method.
Please respond thanks !
raghu!
i mean.. im sry for some fallacies in my presentation of the question. Well, i meant. im able to draw an image to a new location(with graphcisobject.drawimage(..)) but how do i simply delete it. (frm previous location)?
 
You don't delete anything that you draw using GDI+. If you don't want the drawing anymore then you simply don't draw it on the next Paint event. Let's say that you want to draw a rectangle. You would store its location and size in some member variables. In the Paint event read those variable and draw the rectangle they describe.

If you want to move and/or resize the drawing you simply edit those variables and the next time the Paint event is raised the rectangle will get drawn in the new location with the new size. To force a repaint you can call the control's Refresh method or Invalidate and Update.
 
I think it's worth explaining how the system works in more detail...

The OS keeps a buffer with what your form looks like on the screen. It's a static image and that's what is shown on the screen. Whenever a change is requested, it will ask for a repaint. For example, if you move the form out of the screen, then back in the screen again, you'll see it calls your paint event to determine what to show in the parts of the form that newly appear.

The screen moving or resizing is a buffer invalidation raised by the operating system. On the other hand, if you want the user interface to react to the MouseEnter event for example, you will need to invalidate it manually.

Once invalidated, the buffer is not redrawn immediately. It waits for the internal message loop's next update screen iteration. This explains that when you read a large file or performing some other blocking operation, the message loop does not complete until the end of the operation you are performing and the GUI becomes unresponsive. The message loop is stuck!

You can also call the Refresh method to invalidate the buffer AND redraw it immediately without waiting for the next iteration. I do not know about conventions on this, but in most common case, I avoid to use it. The reason is that I prefer to have the GUI become unresponsive altogether rather than having the display update correctly, but not responding to any other event anyway! (mouse, keyboard, etc)
 
Stonkie said:
The OS keeps a buffer with what your form looks like on the screen. It's a static image and that's what is shown on the screen.
This is actually new with Vista, before that all windows painted directly to screen when requested by the WM_PAINT message. A similar functionality has existed long with the ControlStyles.DoubleBuffer to delay paint the whole window at once to screen via internal buffer.
Once invalidated, the buffer is not redrawn immediately. It waits for the internal message loop's next update screen iteration. This explains that when you read a large file or performing some other blocking operation, the message loop does not complete until the end of the operation you are performing and the GUI becomes unresponsive. The message loop is stuck!
A rather good explanation, I'd like to add that Invalidate puts a WM_PAINT message in the regular message queue, so other messages in queue is processed first, but the repaint has now been scheduled and will happen sooner than later.

It is also worth noting that repaints are not requested regularly, they only happen for specific reasons or if your code explicitly request it. This is something that is most noticeable with Vista, now if you for example move a window over another it will no longer invalidate the window beneath, since OS now keeps its own "double buffer" it will just get the buffer image and redraw that to screen instead of requesting the window to draw itself again.
You can also call the Refresh method to invalidate the buffer AND redraw it immediately without waiting for the next iteration.
Yes, Update/Refresh causes a WM_PAINT message to immediately be sent directly to the window, bypassing the regular message queue.
 
Redrawing !

Hi,
I have a query.
Well, i had used the graphcisobject.draw(.....) method to draw an image.
Now, how do i redraw this image on different coordinates???
 
Panel reference

Well,
How do I refer to a panel in a tab while drawing an image using e.graphics.DrawImage(..) . Each tab has a panel and has similar images in it. Now, I've got to update the image in specified panels based on specified inputs.
Could anyone help me on this ?
 
Back
Top