Form ShowDialog won't load image in picture box - c#

When I refresh my form with SHOWDIALOG my image doesn't load in my picture-box but if i refresh with only SHOW it works fine.
I want to be able to refresh with SHOWDIALOG and still have my picture load method work.
I have tried clearing data-bindings of both picture-box and the brows button.
private void formrefresh()
{
FoodItem FoodItem = new FoodItem();
FoodItem.ShowDialog();
this.Close();
}
public void GetImage()
{
OpenFileDialog BrowseImage = new OpenFileDialog();
BrowseImage.Filter = "Image Files(*.jpg; *.gif;)|*.jpg; *.gif";
if (BrowseImage.ShowDialog() == DialogResult.OK)
{
TextBox t =
Application.OpenForms["FoodItem"].Controls["imagePath"] as TextBox;
t.Text = BrowseImage.FileName;
filenametext = BrowseImage.FileName;
PictureBox p = Application.OpenForms["FoodItem"].Controls["foodImage"] as PictureBox;
p.Image = new Bitmap(BrowseImage.FileName);
}
}
private void BrowsImage_Click(object sender, EventArgs e)
{
GetFoodImage image = new GetFoodImage();
image.GetImage();
}

Finding forms (Application.OpenForms) is not a very good approach to use. Try to avoid that if possible. For example, there can be complications if there are multiple instances of a certain Form and you will have to find the exact instance you want to update.
It doesn't make much sense to use a Library to GetImage in your example. If you really need it in a separate Class Library, just return the path. Just return the image path from GetImage method and set the PictureBox from FoodImage.
private void formrefresh()
{
FoodItem foodItem = new FoodItem();
foodItem.ShowDialog();
this.Close();
}
private void BrowsImage_Click(object sender, EventArgs e)
{
GetFoodImage image = new GetFoodImage();
var imagePath = image.GetImage();
this.foodImage.Image = new Bitmap(imagePath);
this.imagePath.Text = imagePath;
}
public string GetImage(FoodItem foodItem)
{
OpenFileDialog BrowseImage = new OpenFileDialog();
BrowseImage.Filter = "Image Files(*.jpg; *.gif;)|*.jpg; *.gif";
if (BrowseImage.ShowDialog() == DialogResult.OK)
{
return BrowseImage.FileName;
}
return "";
}

Related

Picturebox, set and get property to take an image throwing null C#

I have a picturebox that will filled up by OpenFileDialog() after that, I must render the histogram (chart) from it. I use the get and set property to take the image from picturebox to another class or form. But I always getting NullReferenceException. The bitmap seems not having the image after I open a image file, so it's returning nothing. I try to fill the bitmap parameter with full path of an image and it's working, but OpenFileDialog() become pointless.
Click options button:
to render histogram chart:
Here's my code
MainForm.cs
// button for opening image
private void openImage_Click(object sender, EventArgs e)
{
OpenFileDialog img = new OpenFileDialog();
img.Title = "Open Image File...";
img.Filter = "Image File (*.bmp, *.jpg, *.jpeg, *.png |*.bmp;*.jpg; *.jpeg;*.png";
if (img.ShowDialog() == DialogResult.OK) {
pbInput.Image = new Bitmap(img.FileName);
// blablabla
}
}
// set and get property
public Image getImage {
get { return pbInput.Image; }
set { pbInput.Image = value; }
}
OptionsForm.cs
private void hist1_Click(object sender, EventArgs e)
{
h1 = new Histogram();
h1.FormClosed += (s, a) => hist1.Enabled = true;
hist1.Enabled = false;
h1.Show();
}
Histogram.cs
public partial class Histogram : Form
{
MainForm m = new MainForm();
public Histogram()
{
InitializeComponent();
Bitmap b = new Bitmap(m.getImage);
//bla bla bla. . . . . *creating histogram code
}
}
The error message that I got:
I hope this question is clear enough. Thank you..!
PS: English is not my primary language, so apologize for my grammar, etc.
When you write MainForm m = new MainForm(); in your Histogram.cs,
You create a brand new object of Form that doesn't have reference to your old form's image
What you want is to be able to access old form's object reference in your new form or better get that image3 reference in Histogram.cs
One way to do it is to pass it to the constructor
Histogram button
private void hist1_Click(object sender, EventArgs e)
{
h1 = new Histogram(this.getImage);
h1.FormClosed += (s, a) => hist1.Enabled = true;
hist1.Enabled = false;
h1.Show();
}
and then your Histogram form
public partial class Histogram : Form
{
public Histogram(Image image)
{
Bitmap b = new Bitmap(image);
}
}
This will give you the Image in the histogram form.
You should set image to image property
getImage = new Bitmap(img.FileName);
Why don't you directly assign the image to your property?
public Image getImage { get; private set; } // Auto-implemented property.
like this
if (img.ShowDialog() == DialogResult.OK) {
getImage = new Bitmap(img.FileName);
pbInput.Image = getImage;
}
Btw., getImage is not a good name for a property. GetSomething is generally used for methods. Just call your property Image:
public Image Image { get; private set; }

I'm making a photo album viewer but MULTISELECT is only getting one photo. How do it?

This is windows forms. I am trying to make an album viewer where the user selects a picture using openfiledialog and can than view all his selections on a picturebox by clicking the previous and next buttons.
But when I set multiselect to true, only one image displays in the picturebox and I can't navigate through all the selected pictures. (However when I don't use multiselect and choose pictures 1 by 1, they all show in the picturebox.) If anyone is wondering how I display multiple images with a 1 picturebox its because I made previous and next buttons. (They work with one by one selections but not with multiselect).
int pageNumber = 0;
List<Photo> ImageFiles = new List<Photo>();
Photo photo;
private void btnAdd_Click(object sender, EventArgs e) {
OpenFileDialog open = new OpenFileDialog();
open.Multiselect = true;
open.Filter = "Image Files(\*. jpg; \*. jpeg; \*. gif; \*. bmp)|\*. jpg; \*. jpeg; \*. gif; \*. bmp";
if (open.ShowDialog() == DialogResult.OK) {
pctBox.SizeMode = PictureBoxSizeMode.CenterImage;
string textDescrip = txtDescrip.Text;
foreach (Photo photo in open.FileNames) {
photo = new Photo(open.FileName, textDescrip);
ImageFiles.Add(photo);
pctBox.Image = new Bitmap(open.FileName);
}
}
}
Here are my next and previous button click handlers:
private void btnNext_Click(object sender, EventArgs e) {
//if there is no more pages
if (pageNumber > 0) {
// Move to the next page
--pageNumber;
// Load up the PictureBox with the new image.
pctBox.Image = new Bitmap(ImageFiles[pageNumber].fileName);
txtDescrip.Text = ImageFiles[pageNumber].description;
}
else {
MessageBox.Show("No more images to display");
}
}
private void btnPrevious_Click(object sender, EventArgs e) {
if (pageNumber < ImageFiles.Count - 1) {
++pageNumber;
pctBox.Image = new Bitmap(ImageFiles[pageNumber].fileName);
txtDescrip.Text = ImageFiles[pageNumber].description;
}
else {
MessageBox.Show("No more images to display");
}
}
Basically when I click the previous or next buttons, nothing happens and only 1 image shows on the picturebox.

How to make OpenFileDialog variable Global?

I've just recently started with C# and as a project I decided I would try and make a image converter, but I can't seem to get the variable "open" accessible in my button1 context, I'm not looking for comments on how bad my code is, I am just starting.. I just want to get this done and add on to it, I will improve the code later on, thanks. So how can I make it accessible?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void button2_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
// image in picture box
pictureBox1.Image = new Bitmap(open.FileName);
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
// image file path
string path = Directory.GetCurrentDirectory();
textBox1.Text = Path.GetDirectoryName(open.FileName);
}
}
public void Form1_Load(object sender, EventArgs e)
{
this.AutoSize = true;
this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
flowLayoutPanel1 = new FlowLayoutPanel();
flowLayoutPanel1.AutoSize = true;
flowLayoutPanel1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
this.Controls.Add(flowLayoutPanel1);
}
public void button1_Click(object sender, EventArgs e)
{
int selectedIndex = comboBox1.SelectedIndex;
Object selectedItem = comboBox1.SelectedItem;
if ((string)comboBox1.SelectedItem == "*.jpg")
{
pictureBox1.Image.Save(#"" + textBox1.Text + open.FileName + "", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
}
You could move the variable out of the method and into the class, making it an instance field of the class. Then any method would be able to see it. However, there are a few things wrong with that approach:
The OpenFileDialog, like any modal dialog, should be disposed of when you are done with it. Storing the reference in an instance field extends its lifetime until the next time you create a new one or your form is closed, whichever comes first.
All you really need is the file name, not all the other data and resources that the OpenFileDialog carries around, so it's wasteful.
From a user experience point of view, it would be better to give the user a chance to save to a different file anyway. You would be better off presenting a SaveFileDialog to the user, initializing with the currently selected file name.
Sticking with your current UI design, the following is the right way to do what you want:
private string fileName;
public void button2_Click(object sender, EventArgs e)
{
using (OpenFileDialog open = new OpenFileDialog())
{
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
// image in picture box
filename = open.FileName;
pictureBox1.Image = new Bitmap(open.FileName);
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
// image file path
string path = Directory.GetCurrentDirectory();
textBox1.Text = Path.GetDirectoryName(open.FileName);
}
}
}
public void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(fileName))
{
return;
}
int selectedIndex = comboBox1.SelectedIndex;
Object selectedItem = comboBox1.SelectedItem;
if ((string)comboBox1.SelectedItem == "*.jpg")
{
pictureBox1.Image.Save(#"" + textBox1.Text + fileName + "", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
Note the use of using to ensure that the OpenFileDialog instance is disposed of properly when it's no longer needed.
C# don't have global variables, but you can have a workaround for that,
public class GlobalObjects
{
private static OpenFileDialog ofd;
public static OpenFileDialog OpenFileDlg
{
get
{
if (ofd == null)
ofd = new OpenFileDialog();
return ofd;
}
}
}
and call it like,
var fileDlg = GlobalObjects.OpenFileDlg;

c# . if (image == Properties.Resources.image)

I want to do an if stament on an image
if (SortName.Image == Properties.Resources.RadioEmpty)
{
SortName.Image = Properties.Resources.Radio;
}
else
{
SortName.Image = Properties.Resources.RadioEmpty;
}
but its on working any idea what I'm doing wrong ?
o.k additional information
1.
//SortName = A picture box
//Properties.Resources.RadioEmpty = Resources\RadioEmpty.png
//Properties.Resources.Radio = Resources\Radio.png
2.
Nope no errors
3.I wanted to use a custom image for a radio button. A have a picture box with the above code on click. RadioEmpty is the default so I check to see if the picturebox's image is the same as image form the Resources folder is so do code.
i advise you use tag for this issue see this code
private void Form1_Load(object sender, EventArgs e)
{
//in form load the radio is checked or unckecked
//here my radio is unchecked at load
pictureBox1.Image = WindowsFormsApplication5.Properties.Resources.Add;
pictureBox1.Tag = "UnChecked";
}
private void pictureBox1_Click(object sender, EventArgs e)
{
//after pictiurebox clicked change the image and tag too
if (pictureBox1.Tag.ToString() == "Checked")
{
pictureBox1.Image = WinFormsApplication.Properties.Resources.Add;
pictureBox1.Tag = "UnChecked";
}
else
{
pictureBox1.Image = WinFormsApplication.Properties.Resources.Delete;
pictureBox1.Tag = "Checked";
}
}
compare the names. Something like this (unverified)
if (SortName.Image.Name.Equals(Properties.Resources.RadioEmpty.Name))
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap bm1;
Bitmap bm2;
private void button1_Click(object sender, EventArgs e)
{
bm1 = new Bitmap(Properties.Resources.firegirl1);
bm2 = new Bitmap(Properties.Resources.Zemli2);
pictureBox1.Image = bm1;
pictureBox2.Image = bm2;
if (pictureBox1.Image==pictureBox2.Image)
{
MessageBox.Show("Some");
}
else
{
MessageBox.Show("Differ");
}
}
}

Load a bitmap image into Windows Forms using open file dialog

I need to open the bitmap image in the window form using open file dialog (I will load it from drive). The image should fit in the picture box.
Here is the code I tried:
private void button1_Click(object sender, EventArgs e)
{
var dialog = new OpenFileDialog();
dialog.Title = "Open Image";
dialog.Filter = "bmp files (*.bmp)|*.bmp";
if (dialog.ShowDialog() == DialogResult.OK)
{
var PictureBox1 = new PictureBox();
PictureBox1.Image(dialog.FileName);
}
dialog.Dispose();
}
You have to create an instance of the Bitmap class, using the constructor overload that loads an image from a file on disk. As your code is written now, you're trying to use the PictureBox.Image property as if it were a method.
Change your code to look like this (also taking advantage of the using statement to ensure proper disposal, rather than manually calling the Dispose method):
private void button1_Click(object sender, EventArgs e)
{
// Wrap the creation of the OpenFileDialog instance in a using statement,
// rather than manually calling the Dispose method to ensure proper disposal
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "bmp files (*.bmp)|*.bmp";
if (dlg.ShowDialog() == DialogResult.OK)
{
PictureBox PictureBox1 = new PictureBox();
// Create a new Bitmap object from the picture file on disk,
// and assign that to the PictureBox.Image property
PictureBox1.Image = new Bitmap(dlg.FileName);
}
}
}
Of course, that's not going to display the image anywhere on your form because the picture box control that you've created hasn't been added to the form. You need to add the new picture box control that you've just created to the form's Controls collection using the Add method. Note the line added to the above code here:
private void button1_Click(object sender, EventArgs e)
{
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "bmp files (*.bmp)|*.bmp";
if (dlg.ShowDialog() == DialogResult.OK)
{
PictureBox PictureBox1 = new PictureBox();
PictureBox1.Image = new Bitmap(dlg.FileName);
// Add the new control to its parent's controls collection
this.Controls.Add(PictureBox1);
}
}
}
Works Fine.
Try this,
private void addImageButton_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
//For any other formats
of.Filter = "Image Files (*.bmp;*.jpg;*.jpeg,*.png)|*.BMP;*.JPG;*.JPEG;*.PNG";
if (of.ShowDialog() == DialogResult.OK)
{
pictureBox1.ImageLocation = of.FileName;
}
}
You should try to:
Create the picturebox visually in form (it's easier)
Set Dock property of picturebox to Fill (if you want image to fill form)
Set SizeMode of picturebox to StretchImage
Finally:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Open Image";
dlg.Filter = "bmp files (*.bmp)|*.bmp";
if (dlg.ShowDialog() == DialogResult.OK)
{
PictureBox1.Image = Image.FromFile(dlg.Filename);
}
dlg.Dispose();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
if (open.ShowDialog() == DialogResult.OK)
pictureBox1.Image = Bitmap.FromFile(open.FileName);
}
You, can also try like this, PictureBox1.Image = Image.FromFile("<your ImagePath>" or <Dialog box result>);
PictureBox.Image is a property, not a method. You can set it like this:
PictureBox1.Image = System.Drawing.Image.FromFile(dlg.FileName);
You can try the following:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = "Select file to be upload";
fDialog.Filter = "All Files|*.*";
// fDialog.Filter = "PDF Files|*.pdf";
if (fDialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text = fDialog.FileName.ToString();
}
}
It's simple. Just add:
PictureBox1.BackgroundImageLayout = ImageLayout.Zoom;

Categories

Resources