The way I try implement it via standart Windows.Forms (to get valide DialogResult.OK)
System.Windows.Forms.SaveFileDialog dlg = new System.Windows.Forms.SaveFileDialog();
dlg.FileName = "Document"; // Default file name
// dlg.DefaultExt = ".jpg"; // Default file extension
dlg.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
if(dlg.ShowDialog()== System.Windows.Forms.DialogResult.OK)
if (dlg.DialogResult.HasValue && splashDialog.DialogResult.Value)
{
string fName = dlg.FileName;
if (dlg.FileName != "")
{
System.IO.Stream fileStream = (System.IO.FileStream)dlg.OpenFile();
fileStream.Close();
}
}
This is using Windows forms but it saves blank image((
You can do something like that:
var encoder = new JpegBitmapEncoder(); // Or PngBitmapEncoder, or whichever encoder you want
encoder.Frames.Add(BitmapFrame.Create(yourImage));
using (var stream = dlg.OpenFile())
{
encoder.Save(stream);
}
BTW, there is a SaveFileDialog in WPF too, you don't have to use the one from Windows Forms
For WPF code will be like :
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Document";
dlg.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
if (dlg.ShowDialog() == true)
{
var encoder = new JpegBitmapEncoder(); // Or PngBitmapEncoder, or whichever encoder you want
encoder.Frames.Add(BitmapFrame.Create(bi));
using (var stream = dlg.OpenFile())
{
encoder.Save(stream);
}
}
Here bi is the BitmapImage
Related
How do I get C# to force bitmap images that are saved to be saved as 24-bit images as can be seen when you get the right-click properties of the image in Windows. All the images I save are set to 32-bit. I tried the below code with no luck. The source images are all 24-bit as well but are always saved as 32-bit image
public void Save(int index)
{
string savestrFilename;
SaveFileDialog dialog = new SaveFileDialog();
dialog.Title = "image save...";
dialog.OverwritePrompt = true;
dialog.Filter = "JPEG File(*.jpg)|*.jpg|Bitmap File(*.bmp)|*.bmp";
if (dialog.ShowDialog() == DialogResult.OK)
{
savestrFilename = dialog.FileName;
System.IO.FileStream stream = new System.IO.FileStream(savestrFilename , System.IO.FileMode.Create);
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.QualityLevel = 70;
encoder.Frames.Add(BitmapFrame.Create(_grabImageList[index].BitmapImage));
encoder.Save(stream);
stream.Close();
}
}
on buttonclick I'm taking image from file system and save into database, everything is ok but I want when I select image to display that image into pictureBox1
OpenFileDialog open = new OpenFileDialog() { Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg" };
if (open.ShowDialog() == DialogResult.OK)
{
txtPhoto.Text = open.FileName;
}
string image = txtPhoto.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();
byte[] Photo = bimage;
You can use Image property to set the Image for PictureBox Control.
Try This:
DialogResult result= openFileDialog1.ShowDialog();
if(result==DialogResult.OK)
pictureBox1.Image =new Bitmap(openFileDialog1.FileName);
if you want to add it in your code
Complete Code:
OpenFileDialog open = new OpenFileDialog() { Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg" };
if (open.ShowDialog() == DialogResult.OK)
{
txtPhoto.Text = open.FileName;
}
string image = txtPhoto.Text;
Bitmap bmp = new Bitmap(image);
pictureBox1.Image = bmp;//add this line
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();
byte[] Photo = bimage;
Simple code:
picturebox.Image = Bitmap.FromFile(yourimagepath);
OpenFileDialog open = new OpenFileDialog()
{
Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg"
};
if (open.ShowDialog() == DialogResult.OK)
{
PictureBoxObjectName.Image = Image.FromFile(open.FileName);
}
openFileDialog1.Multiselect = false;
openFileDialog1.Filter= "jpg files (*.jpg)|*.jpg";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
foreach (String file in openFileDialog1.FileNames)
{
picturebox1.Image = Image.FromFile(File);
}
}
This is for selection of one image.
var dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Filter = "(*.JPG;*.GIF)|*.JPG;*.GIF";
dlg.ShowDialog();
if (string.IsNullOrEmpty(dlg.FileName)) return;
var fs = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read);
var data = new byte[fs.Length];
fs.Read(data, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
I am using this code but not find how to find size or dimension of image?
Answered as a part of this question: Limit image size
string filename = // get it from OpenFileDialog
var length = new FileInfo(filename).Length;
Image img = Image.FromFile(filename);
var w = img.Width;
var h = img.Height;
use FileInfo given path of this file and use Length as
var dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Filter = "(*.JPG;*.GIF)|*.JPG;*.GIF";
dlg.ShowDialog();
if (string.IsNullOrEmpty(dlg.FileName))
return;
FileInfo info = new FileInfo(dlg.FileName);
Console.Write("Length In Bytes:"+info.Length);
I am new in wpf control and framework. I cant seem to save my images can you help me
Below is my code
SaveFileDialog sfd = new SaveFileDialog();
sfd.FileName = System.IO.Path.GetFileNameWithoutExtension(newlist[currentPicture]);
Nullable<bool> result = sfd.ShowDialog();
if (result == true)
{
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(newlist[currentPicture]);
bmp.Save(newlist[currentPicture]);
}
For a System.Drawing.Bitmap, you need to pass the dialog's FileName property to the Save method.
EDIT: In WPF:
var encoder = new PngBitmapEncoder()
encoder.Frames.Add(BitmapFrame.Create(image));
using (var stream = dialog.OpenFile())
encoder.Save(stream);
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".txt"; // Default file extension
dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension
Nullable<bool> result = dlg.ShowDialog(); // Show open file dialog box
// Process open file dialog box results
if (result == true)
{
// Open document
string filename = dlg.FileName;
txtBrowse.Text = filename;
}
ImageSource imageSource = new BitmapImage(new Uri(txtBrowse.Text));
imgImageAdlut.Source = imageSource; ;
byte[] array1 = null;
//array1 = imageToByteArray();
}
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
Try this:
var img = new System.Drawing.Bitmap(#"D:\Your image path\your image.bmp");
byte[] array1 = imageToByteArray(img);