Image Drawing with Graphics C# [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I want to draw a image in a picture box, and the image is not drawing what is wrong with my code? Thanks
Bitmap b;
private void Form1_Load(object sender, EventArgs e)
{
b = new Bitmap(1000, 100);
pictureBox1.Image = b;
Graphics g = pictureBox1.CreateGraphics();
Rectangle rect = new Rectangle(20, 20, 70, 70);
Image earth = Image.FromFile("" + System.IO.Directory.GetCurrentDirectory() + #"\earth.png");
g.DrawImage(earth,rect);
}

Form.Load fires before anything is drawn to the screen. When the Form is actually displayed for the first time, PictureBox.OnPaint will be called and the control will render itself with its default appearance. To properly draw to a PictureBox, handle its Paint event and do your drawing in the event handler using the supplied Graphics instance. That event will be fired every time any part of the control is redrawn, which will allow you to draw graphics that persist when the control is invalidated. See here for more information.

Related

Possibility of showing System.Drawing.Graphics object in a WPF control [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 28 days ago.
Improve this question
I'm using PrintDocument to print a receipt in for a POS system. I'm using the PrintPage event handler's graphics object to do the printing. The application is written using WPF with .NET 7.
It would be good if I could show a preview in the WPF application before printing. Is there any possibility to display a System.Drawing.Graphics object in a user control? If I could, I can re-use the same logic.
You cannot display a System.Drawing.Graphics object directly. What you can do is draw to a bitmap with Graphics.FromImage and display the bitmap in wpf.
Something like:
var bitmap = new Bitmap(512, 512);
using(var g = Graphics.FromImage(bitmap)){
// Do drawing
}
var bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bitmap.GetHbitmap(), // you will need to delete this hbitmap
IntPtr.Zero,
System.Windows.Int32Rect.Empty,
BitmapSizeOptions.FromWidthAndHeight(512, 512));

How to change the origin in Windows Forms Bitmap? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a Bitmap object in C# Windows Forms. The origin (where axis start) in this Bitmap object is located in the top-left corner. How can I change the origin to the bottom-left corner, like in usual graphs? Thank you!
You can flip the Y coordinates in a picture box by using the code in the following answer;
flip coordinates when drawing to control
private void ScaleTransformFloat(PaintEventArgs e)
{
// Begin graphics container
GraphicsContainer containerState = e.Graphics.BeginContainer();
// Flip the Y-Axis
e.Graphics.ScaleTransform(1.0F, -1.0F);
// Translate the drawing area accordingly
e.Graphics.TranslateTransform(0.0F, -(float)Height);
// Whatever you draw now (using this graphics context) will appear as
// though (0,0) were at the bottom left corner
e.Graphics.DrawRectangle(new Pen(Color.Blue, 3), 50, 0, 100, 40);
// End graphics container
e.Graphics.EndContainer(containerState);
// Other drawing actions here...
}

Winforms multi-layer graphics [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Problem example:
I have a control do draw some geometrical shapes (I use panel).
I have drawn a line.
Now when I move cursor to one of the line tips I want that point to be "highlighted" (a solid circle drawn around it).
And when I move cursor off the point, I want the "highlighting" circle to be erased, but I do not want to redraw the line.
So, technically, I need two layers:
1-st layer to draw my line(s).
2-nd layer to draw/erase highlights.
I do not want the first layer to be redrawn every time when something is drawn/erased in the second layer.
Any suggestions?
Option 1: Nest another Panel in the bottom one. This is good for overlaying graphics including semi-transparency. (Nesting means: panel2.Parent = panel1) You can nest many layers if you want to.
Option 2: Draw into the BackgroundImage of the Panel and use drawing onto the surface for the interactivce stuff.
I noted that you 'don't want to draw the line again'. This is not what you would usually say/do/try when doing graphics. If you are serious about this go for option 2!
See here for the difference of drawing onto a control and into a Bitmap and here for another example of using option 2 to display a cross as a cursor above a Bitmap.
Btw, the two options are not mutually exclusive: You could nest Panels with BackgroundImages and draw interactive stuff on the topmost one..
#TaW pretty much answered it. I set an example:
For static drawing:
private Bitmap myStaticImage;
public Form2()
{
InitializeComponent();
this.myStaticImage = new Bitmap(200, 100);
Graphics g = Graphics.FromImage(this.myStaticImage);
g.DrawLine(Pens.Red, new Point(0, 0), new Point(200, 0));
this.panel1.BackgroundImage = this.myStaticImage;
}
For dynamic drawing:
private void panel1_Paint(object sender, PaintEventArgs e)
{
// Stuff
}
I recommend using a PictureBox or a UserControl with DoubleBuffered property set to true. Panel control is not double buffered and will flicker.

C# panel drawing on button press [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My apologies if I'm missing something simple here, but I've running in circles for some time now. In short, I'd like to update the panel graphics (panel will contain simple shapes like circles and rectangles) every time a button is pressed. The code would look something like this:
private void PanelGraphics()
{
ClearThePanel(); //empties the panel
FillThePanel(); //draws new shapes in the panel
}
The function would be then called every time a button is pressed, and I understand button_events well enough so that's not a problem. However, I have no idea how to pull off the PanelGraphics() function, and a few links about panel_paint events didn't help me much.
You can draw to the panel by creating a graphics controller to it.
Graphics g = panel.CreateGraphics();
You can then use the Graphic class's plethora of methods to draw whatever you want to the panel
g.DrawCurve(parameters);
g.DrawEllipse(parameters);
g.DrawLine(parameters);
g.DrawRectangle(parameters);
To clear the panel the easiest way is to draw a box the colour of the background to the panel
g.DrawRectangle(new Pen(panel.BackColor), new Rectangle(new Point(), panel.Size));

How to print out a pointf in winforms c# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to programming and I want to print a dot in the windows form application.
What is the code if I already have a PointF that is assigned with the x & y?
Thank you very much
You can try using FillRectangle of a Graphics object to fill the rectangle with dimension of 1x1 (looks like a point), because the Graphics object doesn't have any method like SetPixel:
public void SetPixel(PointF p, Color c, Graphics g){
using(Brush brush = new SolidBrush(c)){
e.Graphics.FillRectangle(brush, new RectangleF(p, new Size(1, 1)));
}
}
//Paint event handler of your form
private void Form1_Paint(object sender, PaintEventArgs e){
SetPixel(yourPoint, Color.Red, e.Graphics);
}
Isaac, not a very detailed question, but from what I got is that you have a windows form that you want to draw a "POINT" (1px X 1px dot)? Well its pretty easy if you are using windows forms.
to go the designer view of your form
right click on the background of the form while the form is selected
click properties
click on the events tab (the lightning bolt) of the properties tab
scroll down until you see paint event for that form
type in a function name for that event
browse to your source file for your form
using code from below. There are a couple ways to draw a point I use either a 1px line or a filled 1px box
private void paintEvent(object sender, PaintEventArgs e)
{
// Create a local version of the graphics object for the PictureBox.
Graphics g = e.Graphics;
// Draw a line in the PictureBox.
g.DrawLine(System.Drawing.Pens.Red, 50, 50,
51, 51);
g.DrawRectangle(System.Drawing.Pens.Red, 50, 50, 1, 1);
}

Categories

Resources