Open a image in a new form c# - c#

I have a image showing in
picShowPicture.Image = Image.FromFile(textbox1.Text)
At the moment the image is showing on the main screen what I want is when a user selects a image from the database, it then open up in a new window?
how is this done?

Create a new Form in Designer and pick a PictureBox in it. And create a special method for example
public void SetPicture(Image image)
which will set image to PictureBox.
On selecting picture call:
YourForm form = new YourForm();
form.SetPicture(Image.FromFile(textbox1.Text));
form.ShowDialog();
Or you can dynamically create new form:
Form form = new Form();
PictureBox pictureBox = new PictureBox();
pictureBox.Dock = DockStyle.Fill;
pictureBox.Image = Image.FromFile(textbox1.Text);
pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
form.Controls.Add(pictureBox);
form.ShowDialog();

I'm going to make the assumption that by "selects a image from the database" does not mean that you're (de)serializing BLOBs and just want the code to make the image display in a new window. I will further assume that you have a second form created in your project called "Form2" with an image viewer called "picImageViewerOnForm2".
var newImage = Image.FromFile(textbox1.Text);
var newForm = new Form2();
newForm.picImageViewerOnForm2.Image = newImage;
newForm.Show();

Add Form to application and put PictureBox on it, let's say that ID of PictureBox is "pictureBox1", then create public proterty on that form to access picutre box, like this :
public partial class ShowPictureForm : Form
{
public PictureBox ImagePictureBox { get { return this.pictureBox1; } }
public ShowPictureForm()
{
InitializeComponent();
}
}
then show that new form like this :
ShowPictureForm spf = new ShowPictureForm();
spf.ImagePictureBox.Image.FromFile(textbox1.Text)
spf.ShowDialog();

Add a new Windows Form to your project, named "ShowImageWindow".
Add a picturebox to the window, and the following code:
public Image ImageToShow { get; set; }
public ShowImageWindow()
{
InitializeComponent();
}
private void ShowImageWindow_Load(object sender, EventArgs e)
{
pictureBox1.Image = ImageToShow;
}
Then create and show the window as follows:
Image img = Image.FromFile(textBox1.Text);
ShowImageWindow frm = new ShowImageWindow();
frm.ImageToShow = img;
frm.ShowDialog();
frm.Dispose();

Create a new Form (new type derived from System.Windows.Forms.Form) that accept image path as constructor.
Say new form is ImageForm. Create PictureBox inside this form.
in the function ShowImageWindow (or similar in main form), call like following
ImageForm imageForm = new ImageForm(textbox1.Text)
imageForm.ShowDialog()
In the ctor of ImageForm, set the Image into picturebox control inside ImageForm
public ImageForm(String imagePath)
{
pictureBox1.Image = Image.FromFile(imagePath);
}

Create a new form with just picture box in it.
Then when forming that forms object pass image as parameter(of course you have to create a parameterised constructor of your form) and pass that image in a global image variable.
In Form_Load set that global image variable as your image controls image.

Related

Find image path and view using PictureBox

I have a form application in which I can input a name of an image and view the image in a PictureBox after clicking a button.
private void button1_Click(object sender, EventArgs e)
{
string image = textBox1.Text + ".jpg";
PictureBox pictureBox1 = new PictureBox();
pictureBox1.ImageLocation = image;
}
This is my code but it doesn't do anything, the image I tried to search didn't appear in the PictureBox. What could possibly go wrong? Answers will be appreciated.
Because you've created a new instance of PictureBox but you didn't add it to your form. You should add it to your form's controls like this:
string image = textBox1.Text + ".jpg";
PictureBox pictureBox1 = new PictureBox();
//Set pictureBox1's location on the form
pictureBox1.Location = new Point(10 , 10);
//Add pictureBox1 to your form
Controls.Add(pictureBox1);
Now if your image variable contains a valid image path your PictureBox should show it.
Edit: To write a valid image path in your TextBox try write the full picture's path in your TextBox like below:
D:\Pics\yourPic
Or if you've added it to your project it should be like this:
D:\New folder (1)\WindowsFormsApplication1\WindowsFormsApplication1\yourPic
Just don't forget if you have already placed a PictureBox in your form you just need to call it in the code. You don't need to create a new one. And you should remove this line PictureBox pictureBox1 = new PictureBox();.

Parameter to an image

How to add a parameter for an image to this variable?
var selectionDisplay = new SelectionDisplay(button.Label as string);
Thanks in advance!
EDIT:
I have this set of images and their respective code below (see pic1)
This is where the program gets the images to be displayed. The code for the buttons is this one:
var files = Directory.GetFiles(#".\GalleryImages");
foreach (var file in files)
{
FileInfo fileInfo = new FileInfo(file);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(file, UriKind.Relative);
bi.EndInit();
var button = new KinectTileButton
{
Label = System.IO.Path.GetFileNameWithoutExtension(file),
Background = new ImageBrush(bi)
};
this.wrapPanel.Children.Add(button);
}
This is where the program gets the images to be displayed.
The code for the buttons is this one:
private void KinectTileButtonclick(object sender, RoutedEventArgs e)
{
var button = (KinectTileButton)e.fake_fake_fakeource;
var selectionDisplay = new SelectionDisplay(button.Label as string);
this.kinectRegionGrid.Children.Add(selectionDisplay);
e.Handled = true;
Right now, when i click on of the images, the SelectionDisplay window pops up, which look like this (see pic2). What i want is that when I click an image the SelectionDisplay window should open with the respective image... meaning that if I click on the image with a dog, the window should open with the dog's image, not with other image.
I hope I've made myself clear and that you can help me.
http://i58.tinypic.com/8zl6h3.jpg
http://i57.tinypic.com/208fosy.png
is this the constructor you are talking about? is this where i should make changes? should i add something after "string itemid"?
public SelectionDisplay(string itemId)
{
this.InitializeComponent();
this.messageTextBlock.Text = string.Format(CultureInfo.CurrentCulture,Properties.Resources.SelectedMessage,itemId);
}
I see two approaches:
Just pass the image brush in your constructor. Its view->view, so you aren't breaking MVVM (and it looks like you aren't using that pattern anyways).
new SelectionDisplay(button.Label, button.Background);
Set the path as the "tag" of the button. The tag property is an object you can put whatever you want into (the framework does not use it, and so it is included for this very purpose). Then just pass the string to SelectionDisplay, and instantiate the image just like you are doing for the button:
var button = new KinectTileButton
{
Label = System.IO.Path.GetFileNameWithoutExtension(file),
Background = new ImageBrush(bi),
Tag = file
};
var selectionDisplay = new SelectionDisplay(button.Label as string, button.Tag as string);
FrameworkElement.Tag on MSDN (Note that Button derives from FrameworkElement, as do all controls, so it automatically has it as well!)
UPDATE
I see that SelectionDisplay is a UserControl in your project, so you just need to change its constructor to look like:
Numbers match above:
SelectionDisplay(string labelText, ImageBrush sourceImage)
SelectionDisplay(string labelText, string imagePath)
That is the source of the error you are getting, you have to modify the constructor to take the new parameter.
Please let me know if I can clarify anything.

How do I show a full sized of image, from a click event?

I'm using a GridView to display some tabular data in a WinForm application.
I have a column for the table, which is stored as a VarBinary data type, which contains my image data.
How do I show the full size image, when one of them is clicked on?
I've just had to do similar thing, so I'll just give you the code
Form frm = new Form();
PictureBox pb = new PictureBox();
//pb.Image = Image.FromFile("my.bmp");
pb.Image = myImage;
pb.Dock = DockStyle.Fill;
frm.Controls.Add(pb);
frm.WindowState = FormWindowState.Maximized;
frm.ShowDialog();

How can i replace cursor with bitmap in winform

I am develeoping a magnifier on Mouse move control application in c#.net . I need to replace the cursor with the magnifier control(magnifier control is a picturebox). So is there anyway to accomplish this .
The example code below shows how to set a Cursor on windows form. The same approach can be used to set a Cursor for a Control too.
public class Form_With_A_Cursor_Example {
public void Shows_A_Form_With_A_Cursor_Loaded_From_A_pictureBox() {
Form frm = new Form();
PictureBox pb = new PictureBox() { Image = Image.FromFile( #"C:\Users\xxx\Pictures\someImage.bmp" ) };
frm.Cursor = new Cursor( ( (Bitmap)pb.Image ).GetHicon() );
frm.ShowDialog();
}
}
First add bitmap to your project resources:
Project->projectnameProperties->Add exiting file (from menu next to "Add Resource") add your BMP
Bitmap b = new Bitmap(projectname.Properties.Resources.yourCursorName);
b.MakeTransparent(b.GetPixel(0,0));
Graphics g = Graphics.FromImage(b);
IntPtr ptr = b.GetHicon();
Cursor = new System.Windows.Forms.Cursor(ptr);
Where "projectname" is the name of your project.

Calling a method on another form in C#

I am building a basic Image editor. In my app, if the user wants to resize the image a new form pops up and asks the user to input an new width and height for the image.
public partial class Form1 : Form
{
...
private void resizeToolStripMenuItem_Click(object sender, EventArgs e)
{
resize resizeForm = new resize();
resizeForm.ShowDialog();
}
...
}
I am wondering how I can get the values from the resizeForm and use them to alter the image on the parent form (the Form1 instance).
If this question needs clarification please let me know.
Thanks!
I assume there are a number of ways to do this. I'd probably use public properties on the resizeForm and then get those when the resizeForm.ShowDialog() returns.
if (resizeForm.ShowDialog() == DialogResult.OK) // or whatever
{
myVal = resizeForm.Val;
...
}
or something like that.
Setup properties in your "resize" class for the values you want to retrieve. For example, if you add a width property:
public int Width { get; set; }
you will be able to get the width from your Form1 class.
Add properties to your resize form that your main form can interrogate after the resize form is closed, like ...
DialogResult dr = resizeForm.ShowDialog();
if( dr != DialogResult.Cancel )
{
var newH = resizeForm.Height;
var newW = resizeForm.Width;
// do something with new vals.
}

Categories

Resources