Add pushpin to image - c#

I created a line map and save it as jpeg, I would like to use it for my windows application in c#.
What I wanted to do is click on the specific area of image and and add a pushpin on it and add some information, so that I can click on it later to reveal the information on it. Is this possible?

It's possible to get a specific area inside your picturebox when mouse is clicked. You can do it with this:
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
int xCoordinate = e.X;
int yCoordinate = e.Y;
var picture = new PictureBox
{
Size = new Size(50, 50),
Location = new Point(e.X, e.Y),
Image = Image.FromFile("pushpin.png"),
SizeMode = PictureBoxSizeMode.StretchImage
};
this.Controls.Add(picture);
picture.BringToFront();
}
UPDATE: I've also added how you should add the image.
This should be the result:
Hope it helps!

Related

Refer to Picture Box selected

I am new to C# and want to change the size of some Pictureboxes based on one trackbar Value.
If i write the below code refering to a spesific picturebox (e.g. Picturebox1 instead of PB) it works, but i would like to use one Doubleclick event for all Pictureboxes that i Doubleclick.
The below code gives PB = null.
I get the selected Pictureboxname but how can i refer to this Picturebox?
'''
private void PictureBoxesDoubleClick(object sender, EventArgs e)
{
//get the selected Picturebox name
String PictureBoxName = ((PictureBox)sender).Name;
//This part doesn't work
PictureBox PB = (PictureBox)this.Controls[PictureBoxName];
//Resize the Picture box according to the trackBar Value
PB.Size = new Size(trackBar1.Value, trackBar1.Value);
PB.Left = (this.ClientSize.Width - pictureBox1.Width) / 2;
PB.Top = (this.ClientSize.Height - pictureBox1.Height) / 2;
}
'''
Which pictureBox was selected? C#
Thank you
This is the common function which I used for the two picture boxes on their double click and it is working fine. Check if you also had a common function for all the picture boxes in the form.
private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
//get the selected Picturebox name
String PictureBoxName = ((PictureBox)sender).Name;
PictureBox PB = (PictureBox)this.Controls[PictureBoxName];
//Resize the Picture box according to the trackBar Value
PB.Size = new Size(trackBar1.Value*10, trackBar1.Value*10);
PB.Left = (this.ClientSize.Width - pictureBox1.Width) / 2;
PB.Top = (this.ClientSize.Height - pictureBox1.Height) / 2;
}

How do i reset/clear all the drawings i did on pictureBox1?

I have a pictureBox1 with image inside and when i click on it its drawing points.
Now i added a reset button i called it when i click on it its should clear all the drawings i did on the pictureBox and leavethe image inside without the drawings on it.
I did:
private void button4_Click(object sender, EventArgs e)
{
Graphics graphics;
graphics = pictureBox1.CreateGraphics();
graphics.DrawImage(pictureBox1.Image, 0, 0);
}
So i draw a lot of points on pictureBox1 then click the button and all points are gone but then once i click on the picturebox1 again i see also the new points but also the old points i did before the clearing.
How can i clear the old drawings so it wont show up on the next clicks ?
This is the paint event: Moved the paint event to a new class:
public static void Paint(List<PointF> pb1points, GraphicsPath pb1gp, Point movingPoint, PictureBox pictureBox1, Graphics e)
{
e.Clear(Color.White);
e.DrawImage(pictureBox1.Image, movingPoint);
Pen p;
p = new Pen(Brushes.Green);
foreach (PointF pt in pb1points)
{
e.FillEllipse(Brushes.Red, pt.X, pt.Y, 3f, 3f);
}
using (Pen pp = new Pen(Color.Green, 2f))
{
pp.StartCap = pp.EndCap = LineCap.Round;
pp.LineJoin = LineJoin.Round;
e.DrawPath(pp, pb1gp);
}
}
You can try using Graphics.Clear().
Reference: http://msdn.microsoft.com/en-us/library/system.drawing.graphics.clear(v=vs.110).aspx
setting the Image property to null should work.
picBox.Image = null;
Ii it's not worked ,you might be used the InitialImage property to display your image.
pictBox.InitialImage = null;
Please refer the link:
Clear image on picturebox
This is working:
private void button4_Click(object sender, EventArgs e)
{
Graphics graphics;
graphics = pictureBox1.CreateGraphics();
graphics.DrawImage(pictureBox1.Image, 0, 0);
pb1points = new List<PointF>();
}

Add controls to a c# user control at runtime and be able to manage them

1)
I develop a c# user control.
In that control, I have a button. When the user clicks the button at runtime, a new control (for example, pictureBox) is created ,next to the previous pictureBox.
I did it that way:
PictureBox pb = new PictureBox();
pb.Location = new Point(oldPb.X, oldPb.Y + 100);
pb.Size = oldPb.Size;
Controls.Add(pb);
The problem is, that I want to be able to manage all of the created items.
I want, for example, to index the pictureBoxes, then get a number from the user and change the photo of the wanted photoBox.
for example:
photoBox3.Image = .......
How can I do it?
2)
I want to be able to recognize when the user clicks on one of those photoBoxes and do an action on the chosen photoBox.
How can I do that?
Thanks
List<PictureBox> pictureBoxes = new List<PictureBox>();
for (int i = 0; i < 10; i++)
{
PictureBox pb = new PictureBox();
pb.Location = new Point(.....);
pb.Size = ......;
pb.Click += pb_Click;
Controls.Add(pb);
pictureBoxes.Add(pb);
}
pictureBoxes[3].Image=..... //Use like this
void pb_Click(object sender, EventArgs e)
{
PictureBox pb = sender as PictureBox;
//Do work
}
You can use the Tag Property of the PictureBox to store some kind of index.
You can then have all your PictureBoxes respond to a click event:
pb.Click += new EventHandler(picturebox_Click);
and check the Tag there
private void picturebox_Click(object sender, EventArgs e)
{
PictureBox pb = sender as PictureBox;
if (pb != null)
{
string s = pb.Tag
}
}

Image Button in C# Code behind

I'm trying to set an image background in code-behind. I tried adding a background image to the button, but as soon as I hover over the button, the image disappears. To solve this, I have to write functions to override the button behavior, which is too much to do in code-behind.
I then use an alternative method, that is to add a button and an image separately to a grid cell. The issue is -- when I click on the image, the button won't trigger.
How do I make the button to have the hover and pressed effect, even when, the mouse is either hovering or presses the image on the button, but not the remaining area of the button?
Or hope someone can suggest me a better solution.Below is my code.
InitializeComponent();
Button playBtn = new Button();
playBtn.Width = 60;
playBtn.Height = 30;
Image playIcon = new Image();
playIcon.Source = new BitmapImage(new Uri(#"PATH"));
playIcon.Stretch = Stretch.Uniform;
playIcon.Height = 25;
grid1.Children.Add(playBtn);
grid1.Children.Add(playIcon);
Grid.SetColumn(playBtn, 0);
Grid.SetRow(playBtn, 0);
Grid.SetColumn(playIcon, 0);
Grid.SetColumn(playIcon, 0);
thanks for everyone input, after digging more into it, it sort of work out. What I did is add a Grid to Button.Content then add the image to the Grid. And using Opacity to add the grey out effect for IsEnable false state. Below I post my code, hope someone find it useful or improve on:
Button playBtn = new Button();
Image playIcon = new Image();
public MainWindow()
{
InitializeComponent();
Grid grid2 = new Grid();
RowDefinition grid2_row1 = new RowDefinition();
ColumnDefinition grid2_col1 = new ColumnDefinition();
grid2.RowDefinitions.Add(grid2_row1);
grid2.ColumnDefinitions.Add(grid2_col1);
playBtn.Width = 60;
playBtn.Height = 30;
playBtn.Click += playBtn_Click;
playIcon.Source = new BitmapImage(new Uri(#"pack://PATH..."));
playIcon.Stretch = Stretch.Uniform;
playIcon.Height = 25;
playBtn.Content = grid2;
grid2.Children.Add(playIcon);
grid1.Children.Add(playBtn);
Grid.SetRow(playIcon, 0);
Grid.SetColumn(playIcon, 0);
}
public void playBtn_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello");
}
private void button1_Click(object sender, RoutedEventArgs e)
{
playBtn.IsEnabled = false;
playIcon.Opacity = 0.3;
}
Buttons in WPF have different states = "normal", "mouse over" and "pressed" are three.
When you create the button you are setting up it's "normal" state. You also need to set the "mouse over" state to have the same image as well.
Check the below links
http://blogs.msdn.com/b/knom/archive/2007/10/31/wpf-control-development-3-ways-to-build-an-imagebutton.aspx
http://www.hardcodet.net/2009/01/create-wpf-image-button-through-attached-properties
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/91df562f-414c-4326-ac65-42ef301b5f8f/

Getting correct coordinates from scaled picture box with Zoom SizeMode

I loaded the jpg to picture box (in SizeMode as Zoom).
I drew a rectangle on the picture box and took the coordinate.
I opened the jpg in paint and observed the coordinate (where I drew the rectangle on the picture box).
When I compared the rectangle coordinates (x and y) with paint coordinates, they were not the same.
I changed the SizeMode to Normal and observed that the coordinates became the same, but the image size was too large so it was display partially, so I want to use Zoom SizeMode property.
Say image with size 2825x3538 and keep the picture box size mode as Normal, the image shows partially in picture box. So I changed picture box mode to Zoom (to fit the System screen resolution), and the coordinates missmatched when comparing it with Normal mode with SizeMode.
How can I achieve the same coordinates?
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFD.FileName = "";
OpenFD.Title = "open image";
OpenFD.InitialDirectory = "C";
OpenFD.Filter = "JPEG|*.jpg|Bmp|*.bmp|All Files|*.*.*";
if (OpenFD.ShowDialog() == DialogResult.OK)
{
file = OpenFD.FileName;
image = Image.FromFile(file);
pictureBox1.Image = image;
svc = Screen.PrimaryScreen;
pictureBox1.Width = svc.Bounds.Width;
pictureBox1.Height = svc.Bounds.Height - 100;
mybitmap1 = new Bitmap(pictureBox1.Image);
mybitmap1.SetResolution(300, 300);
pictureBox1.Image = mybitmap1;
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (mybitmap == null)
{
mybitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
mybitmap.SetResolution(300, 300);
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using (g = Graphics.FromImage(mybitmap))
{
using (Pen pen = new Pen(Color.Green, m))
{
e.Graphics.DrawRectangle(pen, r);
e.Graphics.DrawString(lab[c].ToString(), new Font(lab[c].ToString(), 8F), new SolidBrush(label1.ForeColor), r);
}
}
}
You can use two scale factors between the actual image and the picture box, one with height and the other with width.

Categories

Resources