How to check File Size or dimension? - c#

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);

Related

Copy a file but with a "Save as" window

I just want to copy a file to another location. But with a FileDialog.
I tried the code below but this is not working as I want...
Is there any other solution ?
SaveFileDialog saveFile = new SaveFileDialog();
if (saveFile.ShowDialog() == DialogResult.OK)
{
string newDirectory = saveFile.FileName;
System.IO.File.Copy(Path, newDirectory);
}
EDIT :
I want something like this :
Word exemple
A Filedialog with the default name of my source file and the right type. You can see an example when you try to save a Word.
Use the FileStream class . Which is more flexible, especially if you want to track the data transfer process byte by byte.
SaveFileDialog saveFile = new SaveFileDialog();
if (saveFile.ShowDialog() == DialogResult.OK)
{
string newDirectory = saveFile.FileName;
byte[] buffer = new byte[1024 * 1024];
int currentBlockSize = 0;
using (source = new FileStream(Path, FileMode.Open, FileAccess.Read))
{
using (dest = new FileStream(newDirectory, FileMode.CreateNew, FileAccess.Write))
{
while ((currentBlockSize = source.Read(buffer, 0, buffer.Length)) > 0)
{
dest.Write(buffer, 0, currentBlockSize);
}
}
}
}

after selecting image display same image inside picturebox

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.

Read image file in Image object from zip file

I'm using iconic.dll file to read data from compressed files (.zip file extensions)
Please check my code below
string zippath = txtFilePath.Text.Trim() + "\\" + foldername + ".zip";
ArrayList arrFiles = new ArrayList();
using (ZipFile zip = ZipFile.Read(enrollment))
{
foreach (ZipEntry e1 in zip)
{
arrFiles.Add(e1.ToString());
}
}
foreach (string path in arrFiles)
{
Image img1 = Image.FromFile(path); //geting error on this line
imageList.Images.Add(getThumbnaiImage(imageList.ImageSize.Width, img1));
}
How can I read image files from a compressed folder?
Try this :
using (ZipFile zip = ZipFile.Read(enrollment))
{
ZipEntry entry = zip["Image.bmp"];
entry.Extract(outputStream);
}
Also you can show your image in a pricturebox :
PictureBox pb = new PictureBox();
pb.Location = new Point(100, 100);
pb.SizeMode = PictureBoxSizeMode.Zoom;
var bmp = new Bitmap(outputStream);
pb.Image = bmp;
this.Controls.Add(pb);
This seems to be the solution:
using (ZipFile zip = ZipFile.Read(enrollment))
{
foreach (ZipEntry e1 in zip)
{
CrcCalculatorStream reader = e1.OpenReader();
MemoryStream memstream = new MemoryStream();
reader.CopyTo(memstream);
byte[] bytes = memstream.ToArray();
Image img1 = Image.FromStream(memstream);
imageList.Images.Add(getThumbnaiImage(imageList.ImageSize.Width, img1));
}
}

How to save an image to a file in WPF

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);

how i pass image in This function

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);

Categories