How to allow user to change download path? - c#

I'm working on a Winform project,
I have a panel(PNLdownload) the user can download by pressing a button, here's the code:
Bitmap bmp = new Bitmap(PNLdownload.Width, PNLdownload.Height + 120);
PNLdownload.DrawToBitmap(bmp, PNLdownload.Bounds);
bmp.Save(#"C:\Test\Test.bmp");
I want the user to be able to choose where to download the image, how can I do this?
I couldn't find a way to do this on the web

You can use a SaveFileDialog.
Adapting the sample code at the provided link yields:
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
Bitmap bmp = new Bitmap(PNLdownload.Width, PNLdownload.Height + 120);
PNLdownload.DrawToBitmap(bmp, PNLdownload.Bounds);
saveFileDialog1.Filter = "*.bmp";
saveFileDialog1.RestoreDirectory = true;
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if((myStream = saveFileDialog1.OpenFile()) != null)
{
bmp.Save(myStream, ImageFormat.Bmp);
myStream.Close();
}
}

Related

OpenFile Dialog Box keeps resources open

After opening a photo in my application using the Openfile Dialog I cannot do anything with the file unless I close my application. I have placed the OpenFile Dialog in a using statement and tried various ways to release resources with no success. How can I release the process to avoid the error message "The process cannot access the file because it is being used by another process?
using (OpenFileDialog GetPhoto = new OpenFileDialog())
{
GetPhoto.Filter = "images | *.jpg";
if (GetPhoto.ShowDialog() == DialogResult.OK)
{
pbPhoto.Image = Image.FromFile(GetPhoto.FileName);
txtPath.Text = GetPhoto.FileName;
txtTitle.Text = System.IO.Path.GetFileNameWithoutExtension(GetPhoto.Fi‌​leName);
//GetPhoto.Dispose(); Tried this
//GetPhoto.Reset(); Tried this
//GC.Collect(): Tried this
}
}
Your problem is not (OpenFileDialog) your problem is for PictureBox
you can use this for load image or if this not works
do this for load image
OpenFileDialog GetPhoto = new OpenFileDialog();
GetPhoto.Filter = "images | *.jpg";
if (GetPhoto.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(path: GetPhoto.FileName,mode: FileMode.Open);
Bitmap bitmap = new Bitmap(fs);
fs.Close(); // End using
fs.Dispose();
pbPhoto.Image = bitmap;
txtPath.Text = GetPhoto.FileName;
txtTitle.Text = System.IO.Path.GetFileNameWithoutExtension(GetPhoto.Fi‌​leName);
}
As stated in the doc for Image.FromFile:
The file remains locked until the Image is disposed.
So you could try to make a copy of the image and then release an original Image:
using (OpenFileDialog GetPhoto = new OpenFileDialog())
{
GetPhoto.Filter = "images | *.jpg";
if (GetPhoto.ShowDialog() == DialogResult.OK)
{
using (var image = Image.FromFile(GetPhoto.FileName))
{
pbPhoto.Image = (Image) image.Clone(); // Make a copy
txtPath.Text = GetPhoto.FileName;
txtTitle.Text = System.IO.Path.GetFileNameWithoutExtension(GetPhoto.Fi‌​leName);
}
}
}
If it not help, you could try to make a copy via the MemoryStream and the Image.FromStream method: System.Drawing.Image to stream C#

Saving a file in windows forms

I already could open the save file dialog, but when i run the program and tried to save it, it could, but the file is not there. WHy is that? Here is the code:
_saved = false;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "System File (*.pos) | *.pos";
saveFileDialog1.Title = "Save File As";
saveFileDialog1.ShowDialog();
if (_saved)
{
this.Text = "Database - " + saveFileDialog1.FileName + "";
_filename = saveFileDialog1.FileName;
}
else
{
this.Text = this.Text;
}
Thank you, I appreciate your help.
The FileSaveDialog only gives you the UI to choose the file, once the file was choosen by the user you will get the FileName and is now your responsibility to do whatever is necesary with the FileName, such as storing your data and save it.
You should implement the save action by yourself, here is an example.
if(sf.ShowDialog() == DialogResult.OK)
{
using(var fs = new FileStream(sf.FileName,FileMode.Create))
{
// get bytes from text you want to save
byte [] data =new UTF8Encoding().GetBytes(text);
fs.Write(data,0,data.Length);
fs.Flush();
}
}
sf is saveFileDialog1

uploading images in winforms application

Does anyone know if a there is a control to allow the user to upload a image to a windows form? Or any example code to accomplish this.
I am using win-form applications
Thanks,
To allow users to select files in a Windows Forms application you should look into using the OpenFileDialog class.
To use the dialog on your form you will need to find it in the toolbox in Visual Studio and drag it on to your form.
Once associated with the form you can then invoke the dialog from your code like so:
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string selectedFile = openFileDialog1.FileName;
}
You can then use the file path to perform whatever task you wish with the file.
Note: You can use the FileDialog.Filter Property to limit the type of file extensions (images in your case) the user can select when using the dialog.
It's note clear where you are going to upload your image. If you just want to use an image in a simple desktop application you can use OpenFileDialog to allow a user to select an image file. And then you can use this image path in you application. If you you want to upload this image to database you can read this image into memory using something like FileStream class.
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg";
if (open.ShowDialog() == DialogResult.OK)
{
textBox10.Text = open.FileName;
}
cn.Open();
string image = textBox10.Text;
Bitmap bmp = new Bitmap(image);
FileStream fs = new FileStream(image, FileMode.Open, FileAccess.Read);
byte[] bimage = new byte[fs.Length];
fs.Read(bimage, 0, Convert.ToInt32(fs.Length));
fs.Close();
SqlCommand cmd = new SqlCommand("insert into tbl_products(Product_image) values(#imgdata)", cn);
cmd.Parameters.AddWithValue("#imgdata", SqlDbType.Image).Value = bimage;
cmd.ExecuteNonQuery();
cn.Close();
private void cmdBrowser_Click(object sender, EventArgs e)
{
OpenFileDialog fileOpen = new OpenFileDialog();
fileOpen.Title = "Open Image file";
fileOpen.Filter = "JPG Files (*.jpg)| *.jpg";
if (fileOpen.ShowDialog() == DialogResult.OK)
{
picImage.Image = Image.FromFile(fileOpen.FileName);
}
fileOpen.Dispose();
}

How to display an image from file system in a PictureBox in WinForm

I have a small doubt regarding loading an image in PictureBox in WinForms.
I want to show an image file from file system in a PictureBox on my form, say form1.
I am doing Windows applications using C#.
I want to check the file type also say is it pdf/text/png/gif/jpeg.
Is it possible to programmatically open a file from file system using C#?
If anyone knows please give any idea or sample code for doing this.
Modified Code: I have done like this for opening a file in my system, but I don't know how to attach the file and attach the file.
private void button1_Click(object sender, EventArgs e)
{
string filepath = #"D:\";
openFileDialog1.Filter = "Image Files (*.jpg)|*.jpg|(*.png)|*.png|(*.gif)|*.gif|(*.jpeg)|*.jpeg|";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
try
{
}
}
}
I don't know what I have to write in try block. Can anyone help on this?
Use Image.ImageFromFile http://msdn.microsoft.com/en-us/library/system.drawing.image.fromfile.aspx method
Image img = Image.ImageFromFile(openFileDialog1.FileName);
Should work.
EDIT
If you're going to set it to PictureBox, and what to see complete inside it, use picturebox
SizeMode property.
using System.IO;
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = false; //not allow multiline selection at the file selection level
openFileDialog1.Title = "Open Data file"; //define the name of openfileDialog
openFileDialog1.InitialDirectory = #"Desktop"; //define the initial directory
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
try
{
string filename = openFileDialog1.FileName;
FileStream fs=new FileStream(filename, FileMode.Open, FileAccess.Read); //set file stream
Byte[] bindata=new byte[Convert.ToInt32(fs.Length)];
fs.Read(bindata, 0, Convert.ToInt32(fs.Length));
MemoryStream stream = new MemoryStream(bindata);//load picture
stream.Position = 0;
pictureBox1.Image = Image.FromStream(stream);
}
}

c# saving an image of a control

what is the best way to save an image of a control?
currently i am doing this:
chart1.SaveImage(ms, ChartImageFormat.Bmp);
Bitmap bm = new Bitmap(ms);
how would i then prompt the user with a windowsavedialogue and save the BMP to a specific location?
if this is not the best way to do this please suggest a different way
Daok has a nice answer for this.
Adapting Daok's code to change the extension Filter gives you this
chart1.SaveImage(ms, ChartImageFormat.Bmp);
Bitmap bm = new Bitmap(ms);
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = Environment.SpecialFolder.MyDocuments;
saveFileDialog1.Filter = "Your extension here (*.bmp)|*.*" ;
saveFileDialog1.FilterIndex = 1;
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
bm.Save (saveFileDialog1.FileName);//Do what you want here
}
You could prompt them with a SaveFileDialog that would allow them to choose the path and file name and file type where they want to save the file.
Then you just need to write the bmp to a file
Do this:
SaveFileDialog dlg = new SaveFileDialog();
// ... add your dialog options
DialogResult result = dlg.ShowDialog(owner);
if(result == DialogResult.OK)
{
bm.Save(dlg.FileName);
}

Categories