How to delete an image from Graphics - c#

Suppose I use DrawImage a few times to draw a bunch of images.
e.Graphics.DrawImage(newImage, destRect);
How can I delete a specific image from the Graphics paper that I drew it on?
Is there a specific function I can use for deletion?
I have tried dispose and Rectangle.Empty, but they don't actually delete the image I already drew on the paper.

First of all, there's no concept of "delete an object" in GDI+ Graphics. You have to redraw the the entire client area in every frame. You should keep a list of objects and their states in memory and redraw the entire surface in every frame. Beware though, this can lead to flickers and not-so-smooth user experience. Here are a few tips to avoid these:
Make sure your Form or UserControl has its DoubleBuffered property set to True. This will result in a far smoother animation than otherwise.
NEVER EVER call CreateGraphics() to get a reference to the Graphics object in your drawing loop. Update your list of objects and states in your loop and then call Invalidate() on your Control/Form and do the drawing process in Paint event.
One overload of Invalidate() allows you to specify the rectangle that needs to be invalidated (redrawn). You can pass a "safe" rectangle around your bouncing ball's current position (say 20 pixels wider/taller than the ball size) as agrument and then draw only that portion in your Paint event.
To further increase performance you can keep auxilary information such as scores, player names etc. outside the actual "game board" and use normal labels/textboxes for them instead of drawing.

krikara, what you should do is:
Keep track of all the bricks in a list
When the ball hits a brick, remove that brick from the list
Every frame, you must redraw everything from scratch. This includes the ball, the paddle and the list of bricks (and whatever else you need, like score, etc.)
Hope this clears up whatever confusion you had.

Related

How would I go about drawing in a paint program with pressure sensitivity?

I am writing a paint program of sorts, using C# .Net/WinForms and pressure sensitivity is a must. I have everything set up and am getting pressure information from the tablet pen. None of this is an issue.
What I am having trouble approaching is, how would I take your typical, simple paint program (i.e., 'Scribble') and draw using the pressure data I am getting? Scribble-type simple apps track current and previous cursor positions, while drawing is enabled, and draws lines between them. But each point laid down by capturing cursor positions is drawn between using a fixed width line.
Assuming I have all the data I need: x y positions and pen pressure, how would I begin to think about drawing between points that should be different widths?
I would break your total xy positions in to many smaller xy positions (maybe a pixel or two in length) and apply the width accordingly corresponding to the pen pressure.
The easiest way to do it is to keep track of the previous point and the previous size (based on pressure) and then draw a lineto from the previous point to the current point, using the previous size for the line size.
Even using GDI+ is fast this way, if you make sure not to create new objects in the event handler code.
The other way I mentioned is much more advanced (though may yield better results) and I may end up implementing it at a later date, when I do everything vector-based.

Optimal 2D Tile rendering in C#

So I'm making a simple 2D Sidescrolling game in C# however I've found that using Graphics.drawImage doesn't particularly allow me to update the tiles as I wish. For example, I tell it to draw the image and it stays where I tell it to be. I want to be able to move the entire scene left to right. This would be easier if I had to use a for loop or something and define it's position every time it draws the image.
This may be confusing and I'm certain there's a way of doing it, I just don't know how.
So my question to you is: How can I control the positioning of each rectangle drew on a form so that I can scroll the entire scene to the left when I wish?
I'm certainly no game/graphics expert, so take this with a grain of salt.
A couple things spring to mind.
First, you could pre-render the entire level as one bitmap, then just paint the relevent portion into your picturebox.
Second, the Graphics class has a Transforms property (I may have the name wrong). You could add a linear transformation to the graphics object so the painting coordinates of your tiles wouldnt change, but the graphics object would slide, or I think the graphics term is "translate" the output en-masse.

Dynamically resize texture on the fly

I'm using RenderTarget2D to draw my map to before rendering it to the screen as it changes very rarely and the map itself is made up of a LOT of very small tiles. So rather than drawing all tiles to the buffer every frame, I'm drawing them to a RenderTarget2D which I then draw to the buffer.
My question is in regards to the RenderTarget2D "texture". If the player was to resize the window, which I want to enable at least to play with a little, what is the proper way to modify the RenderTarget2D object in regards to dimensions?
At the moment I'm just recreating the object anytime the window is resized which may be fine, but I figured I should ask to be safe I'm not missing something simpler.
texMap = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
There is no way to resize a render target after it's been created. Call Dispose() on the existing render target, if it exists, and then create a new one.

Windows Phone 7 onDraw

I am developing a game. I would like to draw a couple of textures on the screen. I succesfully achieved that by drawing texture2d using spriteBatch.
The thing is the textures are displayed only if I put the code in the onDraw method. The onDraw method is bound to the timer so it executes many times. I would like to draw my rectangles only once.
When I put the code in the constructor the rectangles are not displayed - they show up only when I put the code in the onDraw function. How can I omit that?
There isn't really any sensible option for doing this in XNA. Most games want to re-draw the entire screen each frame - so that is how XNA is structured. (And, without a really compelling reason, this is how you should structure your game too.)
XNA is double-buffered (I don't think there's a way to turn that off). You do your drawing on the back-buffer and then swap it with the front buffer. You never draw to the screen directly.
So, while you don't have to clear the screen and re-draw it on each frame, if you don't you must manually keep the contents of these two buffers in-sync - otherwise you will get severe flickering. This is not worth the effort.
What you may be looking for is the Game.SupressDraw method (call it from Update - or - as an alternative: override BeginDraw and return false). This will prevent Draw from being called for that particular frame, and prevent the back-buffer from being swapped to the front. So the previous frame simply stays on-screen.
But it's generally easier to simply draw every single frame.

Erase part of RenderTarget when drawing primitives?

I'm creating a paint like application using XNA.
I have a render target which acts as a canvas. When the user draws something I draw corresponding triangles using DrawUserPrimitives and triangle strips to make lines and other curves.
I want to implement an eraser in the application, so that the user can erase the triangles from the texture. I've used OpenGL in the past and there I would just use a blend function like so: glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_ALPHA);
How would I do this in XNA? I tried setting the GraphicsDevice blend mode to AlphaBlend, Additive, etc.. but it did not work. Any ideas?
If you change your code to use a Texture2D instead of a RenderTarget2D, you can use RenderTarget2D.GetTexture() to get the pixel data from the RenderTarget2D.
Of course, there will probably be a performance hit, but if you can optimize the code in a way that cuts down on the number of times this is done, like temporarily drawing the background color instead of actually erasing, and then iterating through the pixels after the mouse is released, you could make it work.
There may be a better way, but I couldn't find it.

Categories

Resources