im doing a painter project in c# in windows forms application and im trying to save the drawing and upload it the picturebox . the image is load, but im not able to keep working on it . is there a way to save and load the a bitmap in 'mdl' format or something else so i could keep working on it ?
this is how i did it :
private void btn_save_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = Directory.GetCurrentDirectory();// + "..\\myModels";
saveFileDialog1.Filter = "Images (.bmp;.jpg;.png)|.bmp;.jpg;.png";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
bm.Save(saveFileDialog1.FileName, ImageFormat.Jpeg);
}
}
private void btn_Load_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(.jpg; *.jpeg; *.gif; *.bmp)|.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
pic.Image = new Bitmap(open.FileName);
pic.Refresh();
}
}
Related
My code so far works, except that the previous and next button only works for 2 images. Why? I don't get it,the list should be holding everything then why?
If I add 10 images, I wanna press previous and next all the way through the images.
int PageNumber = 0;
// A list of image filenames to display. You could populate this by
reading filenames from disk
List<string> ImageFilenames = new List<string>();
private void btnAdd_Click(object sender, EventArgs e)
{
// open file dialog
OpenFileDialog open = new OpenFileDialog();
open.Multiselect = true;
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg;
*.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
pctBox.SizeMode = PictureBoxSizeMode.CenterImage;
// display image in picture box
ImageFilenames.Add(open.FileName);
pctBox.Image = new Bitmap(open.FileName);
// image file path
//textBox1.Text = open.FileName;
}
}
private void btnPrevious_Click(object sender, EventArgs e)
{
//if there is no more pages
if (PageNumber == ImageFilenames.Count - 1)
{
// Move to the next page
--PageNumber;
// Load up the PictureBox with the new image.
pctBox.Image = new Bitmap(ImageFilenames[PageNumber]);
}
}
private void btnNext_Click(object sender, EventArgs e)
{
// If we're not yet on the last page...
if (PageNumber < ImageFilenames.Count - 1)
{
// Move to the next page
++PageNumber;
// Load up the PictureBox with the new image.
pctBox.Image = new Bitmap(ImageFilenames[PageNumber]);
}
}
Changed your logic a bit. Please find the code for reference
int nTotalNumber = 0;
int nCurrentItem = 0;
List<string> ImageFilenames = new List<string>();
private void LoadImage()
{
using (OpenFileDialog open = new OpenFileDialog())
{
open.Multiselect = true;
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp;*.png)|*.jpg; *.jpeg; *.gif; *.bmp; *.png";
if (open.ShowDialog() == DialogResult.OK)
{
string sFileName = open.FileName;
ImageFilenames = open.FileNames.ToList();
}
pbBox.Image = Image.FromFile(ImageFilenames[0]);
}
if (ImageFilenames.Count > 0)
nTotalNumber = ImageFilenames.Count;
}
private void btnPrevious_Click(object sender, EventArgs e)
{
nCurrentItem--;
if (nCurrentItem < 0)
nCurrentItem = 0;
else if (nCurrentItem < nTotalNumber)
pbBox.Image = Image.FromFile(ImageFilenames[nCurrentItem]);
}
private void btnNext_Click(object sender, EventArgs e)
{
nCurrentItem++;
if (nCurrentItem > nTotalNumber)
nCurrentItem = nTotalNumber;
else if (nCurrentItem < nTotalNumber)
pbBox.Image = Image.FromFile(ImageFilenames[nCurrentItem]);
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.png; *.bmp)|*.jpg; *.jpeg; *.gif; *.png; *.bmp";
if(open.ShowDialog() == DialogResult.OK)
{
tbFileName.Text = open.FileName;
pictureBox1.Image = new Bitmap(open.FileName);
}
}
So I want to make an if statement, if the image is too big for the initial size of the image box (520, 301), set the image box sizemode to autosize, otherwise just put it in there.
I'm pretty sure that you can change it by using this:
picturebox1.SizeMode = PictureBoxSizeMode.AutoSize;
But I don't know how to write the if statement.
Just load your file to Bitmap and then compare its Height and Width property with our custom size (500 x 301). like
...
tbFileName.Text = open.FileName;
using (Bitmap bmp = new Bitmap(open.FileName))
{
if (bmp.Height >= 301 && bmp.Width >= 500)
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox1.Image = bmp;
}
You could store your image temporary before assigning it to your picturebox and compare the size of it with the size of your box.
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.png; *.bmp)|*.jpg; *.jpeg; *.gif; *.png; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
Bitmap tmp = new Bitmap(open.FileName);
if(tmp.Height >= pictureBox1.Height || tmp.Width >= pictureBox1.Width)
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox1.Image = tmp;
}
}
I Am programming in C# and I Already have it that I can select my files from my phone with this text:
private void Button_Click_2(object sender, RoutedEventArgs e)
{
var FileOpenPicker = new Windows.Storage.Pickers.FileOpenPicker();
FileOpenPicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.List;
FileOpenPicker.FileTypeFilter.Add(".jpg");
FileOpenPicker.FileTypeFilter.Add(".jpeg");
FileOpenPicker.FileTypeFilter.Add(".png");
FileOpenPicker.PickSingleFileAndContinue();
How can I let the picture popup in the picturebox?
Use a FileDialog and load a bitmap from the selected path:
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "bmp files (*.bmp)|*.bmp";
if (dlg.ShowDialog() == DialogResult.OK)
{
// 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);
}
}
I have been trying to save the image in pictureBox1. What am trying to do is to make it so people can "Save" the image from pictureBox1 by clicking on my "Save As" Button.
I am using Visual Studio 2010 with C#
This is what I have so far:
private void Button2_Click(System.Object sender, System.EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Png Image|*.jpg";
saveFileDialog1.Title = "Save an Image File";
saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
pictureBox1.Image = new Bitmap ("c:/avatar.png");
{
SaveFileDialog dialog = new SaveFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image.Save(#"C:\Documents and Settings\.Png", System.Drawing.Imaging.ImageFormat.Png);
}
}
}
I'm not sure what you are trying with your code, there seems to be quite a few things wrong in there. This is how I would do it:
private void Button2_Click(System.Object sender, System.EventArgs e)
{
if (pictureBox.Image != null)
{
using {var dialog = new SaveFileDialog())
{
dialog.Title = ...
saveFileDialog1.Filter = "Png Image|*.png";
...other properties...
if (dialog.ShowDialog == DialogResult.OK)
{
pictureBox.Image.Save(dialog.FileName, System.Drawing.Imaging.ImageFormat.Png)
}
}
}
}
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;