Hey guys i'm trying to figure out how to draw a bitmap that i have loaded onto my form.
I am using this following code to draw it
private void button1_Click(object sender, PaintEventArgs e)
{
open.ShowDialog();
dir.Text = open.FileName.ToString();
image = new Bitmap(dir.Text);
e.Graphics.DrawImage(image, 85, 38);
}
Using this nothing gets drawen, am i not using the correct draw method?
I don't think you really want to paint anything yourself. Try adding a picturebox to your form, and in the button click event, simply set the image to that picturebox.
// In initializecomponent()
button1.Click += button1_Click;
// The click handler
private void button1_Click(object sender, EventArgs e)
{
if (open.ShowDialog() == DialogResult.OK)
{
dir.Text = open.FileName.ToString();
image = new Bitmap(dir.Text);
pictureBox1.Image = image;
}
}
Related
i am planning to write something on a picture box.in the page load event i am able to do that.
Graphics g = Graphics.FromImage(pictureBox1.Image);
Font font = new Font("Courier New", 6);
payagainst = "PARADISE TRADING, CONT & REAL ESTATE";
g.DrawString(payagainst, font, new SolidBrush(Color.Black), pagainstX, pagainstY);
My requirement is there is two textbox in the form ie to mention the the X and Y coordinates of the newly drawn label. If the user changes the values in the textbox position of the label has to be changed according to the values given in the text box. in the text box leave event i have written the code as follows. but its not working. whats the correct method to achieve this.
private void txtpaX_Leave(object sender, EventArgs e)
{
if (flag != 0)
{
Graphics gs = Graphics.FromImage(pictureBox1.Image);
Font font = new Font("Courier New", 6);
payagainst = "PARADISE TRADING, CONT & REAL ESTATE";
amount = 3300;
gs.DrawString(payagainst, font, new SolidBrush(Color.Black), float.Parse(txtpaX.Text), float.Parse(txtpaY.Text));
flag = 1;
}
}
any help will be appreciated
What is supposed to happen after the string is printed over the image?
Do you need to save the image or pass it on or is the display in the PictureBox the only aim?
For the latter case this is the way to do it:
You place the drawing code only in the Paint event and call it, by invalidating the PictureBox, either whenever the TextBoxes have a change or when you leave them..:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawString(yourText, yourFont, yourBrush,
float.Parse(textBox1.Text), float.Parse(textBox2.Text));
}
// this will show up only after leaving the textbox:
private void textBox_Leave(object sender, EventArgs e)
{
pictureBox1.Invalidate();
}
// this will provide a live change:
private void textBox_TextChanged(object sender, EventArgs e)
{
pictureBox1.Invalidate();
}
Note that you should hook this code to both TextBoxes!
This code will persist the drawing but it will not change the actual Bitmap. Which I guess wouldn't make sense until you know exactly where the text should go..
If you want to change the actual Image you can do it like this:
private void stampButton_Click(object sender, EventArgs e)
{
using (Graphics G = Graphics.FromImage(pictureBox1.Image))
G.DrawString(yourText, yourFont, yourBrush,
float.Parse(textBox1.Text), float.Parse(textBox2.Text));
}
..and then maybe save it like this:
private void saveButton_Click(object sender, EventArgs e)
{
pictureBox1.Image.Save("yourfilename", System.Drawing.Imaging.ImageFormat.yourfileformat);
}
I have a panel where I am drawing images on button click. But on the second button click, the previously drawn image is being replaced by the new Image.
void panel_Image_Paint(object sender, PaintEventArgs e)
{
if (Clipboard.ContainsImage())
{
Point p1 = new Point(i, 0);
e.Graphics.DrawImage(Clipboard.GetImage(), p1);
i += img.Width;
}
}
I want to retain the previously drawn image in the panel, when the new image is being drawn. The clipboard is being refreshed on each button click with the new image. Any help would be highly appreciated!!!!
Thanks..
I'm not sure how the paint event actually works and what causes your images to disappear , but what you can try is adding your images to a list and then looping through the list to get all of your images displayed on the panel.
Try this code:
int i = 0;
List<Image> Images = new List<Image>();
private void panel1_Paint(object sender, PaintEventArgs e)
{
if (Clipboard.ContainsImage())
{
Images.Add(Clipboard.GetImage());
foreach (Image item in Images)
{
e.Graphics.DrawImage(item, new Point(i,0));
i += Clipboard.GetImage().Width;
}
}
i = 0;
}
Don't forget to call the Invalidate function.
private void button1_Click(object sender, EventArgs e)
{
panel1.Invalidate();
}
I am trying to recreate a mac style menu bar is c# on visual studio 2012. I can get the image to move on mouseEnter and relocate back to the original place on mouseLeave. I do this by setting the location of the picturebox.
The problem I have is when I mouseEnter the image if I leave the mouse in the area at the bottom of the image between the bottom of the old image location and the bottom of the new image location the image will jump constantly between the two location.
Can anyone advise how to stop this or avoid it.
private void pic1_MouseEnter(object sender, EventArgs e)
{
pic1.Location = new Point(328, 300);
}
private void pic1_MouseLeave(object sender, EventArgs e)
{
pic1.Location = new Point(328, 316);
}
Try to detach event handler before moving image location, then attach it back after. Something like this :
private void pic1_MouseEnter(object sender, EventArgs e)
{
pic1.MouseEnter -= pic1_MouseEnter;
pic1.MouseLeave -= pic1_MouseLeave;
pic1.Location = new Point(328, 300);
pic1.MouseEnter += pic1_MouseEnter;
pic1.MouseLeave += pic1_MouseLeave;
}
True, since moving the image triggers the MouseLeave event, Leave callback sends it to its original location, thus triggering MouseEnter and so on for ever and ever (unless you move the mouse away).
I would suggest you eliminate MouseLeave callback and keep a state of where the image is:
private bool retracted = false;
private void pic1_MouseEnter(object sender, EventArgs e)
{
if (retracted)
{
pic1.Location = new Point(328, 316);
}
else
{
pic1.Location = new Point(328, 300);
}
retracted = !retracted;
}
Thanks for all the solutions but none had the desired effect but I have come up with a solution using mouseHover and mouseLeave. On hover moves the image to the desired location ad the leave returns it to the original spot.
private void pic1_MouseLeave(object sender, EventArgs e)
{
pic1.Location = new Point(328, 316);
}
private void pic1_MouseHover(object sender, EventArgs e)
{
pic1.Location = new Point(328, 310);
}
I have a pictureBox with an image inside.
I want, when i click on a button the image should hide and click again to show the image.
In the pictureBox, using paint event i am drawing some lines.
So if im doing pictureBox1.Refresh(); it will draw the lines. I want that if i click on a button the image will not display on/off.
pictureBox1 = null; or pictureBox1.Image.Dispose(); doesn't work it's showing me big red x with white background.
To hide it:
pictureBox.Visible = false;
To hide/show it in a click event:
void SomeButton_Click(Object sender, EventArgs e)
{
pictureBox.Visible = !pictureBox.Visible;
}
For toggling the image in your PictureBox you can create a 1 pixel bitmap and assign it to the picture box when you want hide your image, then assign your image back again. I am a little unclear of what the second part of your question is asking, any drawing in the picturebox's Paint Event will remain unless you exclude it in the Paint Event based on some Condition. If you want to draw a line in the box an toggle it on/off from a button see my second example.
i.e.
public partial class Form1 : Form
{
Bitmap nullBitmap = new Bitmap(1, 1); // create a 1 pixel bitmap
Bitmap myImage = new Bitmap("Load your Image Here"); // Load your image
bool showImage; // boolean variable so we know what image is assigned
public Form1()
{
InitializeComponent();
pictureBox1.Image = myImage;
showImage = true;
}
private void button1_Click(object sender, EventArgs e)
{
if (showImage)
{
pictureBox1.Image = nullBitmap;
showImage = false;
}
else
{
pictureBox1.Image = myImage;
showImage = true;
}
}
}
Second Example
public partial class Form1 : Form
{
bool showLines;
public Form1()
{
InitializeComponent();
showLines = true;
}
private void button1_Click(object sender, EventArgs e)
{
if (showLines)
{
showLines = false;
pictureBox1.Invalidate();
}
else
{
showLines = true;
pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if(showLines)
e.Graphics.DrawLine(Pens.Purple, 0, 0, 100, 100);
}
}
picturebox1.BackgroundImage = null
Bitmap hh = (Bitmap)System.Drawing.Bitmap.FromFile("example.png");
Graphics.FromImage(hh);
IntPtr ptr = hh.GetHicon();
Cursor c = new Cursor(ptr);
this.Cursor = c;
I use this code to create a custom image cursor. I want to retrieve the coordinates of this custom image cursor when on a Click event. So that these coordinates can be used to draw the image of this cursor in a picture box when clicked on the image loaded in the picture box. I'm doing this in C#.
I tried another approach
public partial class Form1 : Form
{
private Bitmap _bmp = new Bitmap(250, 250);
public Form1()
{
InitializeComponent();
panel1.MouseDown += new MouseEventHandler(panel1_MouseDown);
panel1.Paint += new PaintEventHandler(panel1_Paint);
using (Graphics g = Graphics.FromImage(_bmp))
g.Clear(SystemColors.Window);
}
private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Point mouseDownLocation = new Point(e.X, e.Y);
label1.Text = mouseDownLocation.X.ToString();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(_bmp, new Point(0, 0));
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
using (Graphics g = Graphics.FromImage(_bmp))
{
g.DrawString("Mouse Clicked Here!", panel1.Font, Brushes.Black, e.Location);
}
panel1.Invalidate();
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Image.Save(#"C:\test.jpg", ImageFormat.Jpeg);
}
But when i try so save the image i get an Exception: Object reference not set to an instance of an object.
Please note that panel1 in the code above refers to a picture box
To get the coordinates of the mouse on a PictureBox you should not handle the OnClick event but the OnMouseDown, for example in this way:
private void pb_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Point mouseDownLocation = new Point(e.X, e.Y);
}
now you have the mouseDownLocation which contains the coordinates you were looking for.
i know the way to get the coordinate of mouse you can code it like
Cursor.Position.X and Cursor.Position.Y to get the Coordinate under the mouse