.NET Framework Developer Center >
.NET Development Forums
>
.NET Base Class Library
>
Drawing a Bitmap
Drawing a Bitmap
- Hello
I think this question is not difficult but i'm not experienced in C# and I cannot find the solution. I have a Bitmap Object and want to draw this image.
IStrImg vsa = VM.VSAccess.Sdk.GetImage("E:\\schnitte\\E9056-07-II-PAS\\E9056-07-II-PAS.vsf"); Bitmap bm = vsa.GetImagePart(recX, recY, width, height, sizeX, sizeY); rect = new Rectangle(0, 0, bm.Width, bm.Height); Graphics.DrawImage(bm, rect);When i run this code, i always get an error:
error CS0120: An object reference is required for the nonstatic field, method, or property 'System.Drawing.Graphics.DrawImage(System.Drawing.Image, System.Drawing.Rectangle)'
I don't know what to do here and I hope someone can help me.
Kind regards,
ClaudeMichelle
Answers
- Hi again,
with this code you get the graphics context of the bitmap and paint on that bitmap.
Get the device context of your form ( i guess thats the place you want to paint the image ) by using:
Graphics oGraphic = Graphics.FromHwnd(this.Handle);
If you have got questions about this, just ask.
Mark the thread as answered if the answer helps you. This helps others who have the same problem !
C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/- Marked As Answer byeryangMSFT, ModeratorFriday, November 20, 2009 11:38 AM
- Hello Claude,I just want to clarify something for you: System.Drawing.Graphics is not used for painting directly to the screen. Think of it as the artist standing in front of a canvas. What you have to recognize is that you are responsible for coming up with the canvas, and then you are responsible for displaying the canvas to the user.System.Drawing.Graphics has many methods that allow you to draw to the canvas. So, for example, you could load an image from any stream or file location, create your graphics object from that, create a semi-transparent watermark and over lay it, draw some text, add some geometry, save out your image (or write it to a stream, cache it, etc) and then dispose your graphics object.Without a little hacking, you're not going to be able to display an image in the console. You'll need a winforms app (or web page or silverlight or WPF etc) to see something.I created a simple solution and zipped it up for you on my blog:In that solution I stubbed in some simple performance metrics as you indicated you were interested in above. I'm not sure from your posts exactly what you are looking for, but that should get you started.There are two approaches; note that one does not dispose the graphics object as it was 'given' to you by the paint event.Hope this helps.Cheers,James
Me, coding and stuff: Mr. James- Marked As Answer byClaudeMichelle Sunday, November 08, 2009 8:05 PM
All Replies
- DrawImage has to be called on an instance of a Graphics object. You are calling it like a static method.
- How do I get such an Graphics object?
I tried,
IStrImg vsa = VM.VSAccess.Sdk.GetImage("E:\\schnitte\\E9056-07-II-PAS\\E9056-07-II-PAS.vsf"); Bitmap bm = vsa.GetImagePart(recX, recY, width, height, sizeX, sizeY); rect = new Rectangle(0, 0, bm.Width, bm.Height); Graphics g = Image.FromImage( bm );because I thought that should be possible but it does not seem so...
Are there any other possibilities? - Hi,
open your Object browser and search for graphics. Take a look at the .From...() methods to get a graphic object.
Like:
System.Drawing.Graphics.FromHwnd
System.Drawing.Graphics.FromHdc
...
If you have got questions about this, just ask.
Mark the thread as answered if the answer helps you. This helps others who have the same problem !
C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/ - Thanks Heslacher
I changed the code to,
IStrImg vsa = VM.VSAccess.Sdk.GetImage("E:\\schnitte\\E9056-07-II-PAS\\E9056-07-II-PAS.vsf"); Bitmap bm = vsa.GetImagePart(recX, recY, width, height, sizeX, sizeY); Rectangle rect = new Rectangle(0, 0, bm.Width, bm.Height); Graphics g = Graphics.FromImage(bm); g.DrawImage(bm, rect);I have no compiler error or exceptions, but i cannot see the image yet.
There just opens a dos windows for 2 seconds and the program terminates. - Hi again,
with this code you get the graphics context of the bitmap and paint on that bitmap.
Get the device context of your form ( i guess thats the place you want to paint the image ) by using:
Graphics oGraphic = Graphics.FromHwnd(this.Handle);
If you have got questions about this, just ask.
Mark the thread as answered if the answer helps you. This helps others who have the same problem !
C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/- Marked As Answer byeryangMSFT, ModeratorFriday, November 20, 2009 11:38 AM
- Hello and thanks again
I started a new project Windows project with VS2005 because the other project was just a console application and it had no this.Handle member.
The new Code looks like this:
namespace ReferenceWindowsSlide { class Program : System.Windows.Forms.Form { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Program pro = new Program(); pro.GetSlidePart(0, 0, 5000, 5000, 600, 600); Application.Run(pro); } public void GetSlidePart(int x, int y, int w, int h, int scrw, int scrh) { int recX, recY, width, height, sizeX, sizeY; Bitmap bm; Rectangle rect; recX = x; recY = y; width = w; height = h; sizeX = scrw; sizeY = scrh; IStrImg vsa = VM.VSAccess.Sdk.GetImage("E:\\schnitte\\E9056-07-II-PAS\\E9056-07-II-PAS.vsf"); bm = vsa.GetImagePart(recX, recY, width, height, sizeX, sizeY); rect = new Rectangle(0, 0, bm.Width, bm.Height); Graphics g = Graphics.FromHwnd(this.Handle); g.DrawImage(bm, rect); } } }It compiles and it runs, but still i see no image. - Hi,
thats based on your design i guess ( i am VB ). I would suggest that you do something simple like place a button on your form and call this GetSlidePart() method in the click event of the button. Or if you want to code a slideshow, use it inside a timer event.
If you have got questions about this, just ask.
Mark the thread as answered if the answer helps you. This helps others who have the same problem !
C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/ - Hello
What i need is a program, which i start and that then just opens an image. I need it just to make a performance test. I think the best solution is to do this with a console application.
I just made the forms application, because of the Handle object. When there is a possibility to get such a Handle in a console application, i would prefer the console application.
- Hello Claude,I just want to clarify something for you: System.Drawing.Graphics is not used for painting directly to the screen. Think of it as the artist standing in front of a canvas. What you have to recognize is that you are responsible for coming up with the canvas, and then you are responsible for displaying the canvas to the user.System.Drawing.Graphics has many methods that allow you to draw to the canvas. So, for example, you could load an image from any stream or file location, create your graphics object from that, create a semi-transparent watermark and over lay it, draw some text, add some geometry, save out your image (or write it to a stream, cache it, etc) and then dispose your graphics object.Without a little hacking, you're not going to be able to display an image in the console. You'll need a winforms app (or web page or silverlight or WPF etc) to see something.I created a simple solution and zipped it up for you on my blog:In that solution I stubbed in some simple performance metrics as you indicated you were interested in above. I'm not sure from your posts exactly what you are looking for, but that should get you started.There are two approaches; note that one does not dispose the graphics object as it was 'given' to you by the paint event.Hope this helps.Cheers,James
Me, coding and stuff: Mr. James- Marked As Answer byClaudeMichelle Sunday, November 08, 2009 8:05 PM
- Hi again,
so if you don`t need to show the image at all, then the first two lines of code in your initial post/question will fit for a performance test.
If you have got questions about this, just ask.
Mark the thread as answered if the answer helps you. This helps others who have the same problem !
C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/ - Thanks a lot Heslacher and Mr James
It works now.


