Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Convert Image File into Byte Array in C#
- public static byte[] ImageToByteArrayFromFilePath(string imagefilePath)
- {
- byte[] imageArray = File.ReadAllBytes(imagefilePath);
- return imageArray;
- }
- Convert Image to Byte Array in C# using ImageConverter
- public static byte[] ImageToByteArray(string imagefilePath)
- {
- System.Drawing.Image image = System.Drawing.Image.FromFile(imagefilePath);
- byte[] imageByte = ImageToByteArraybyImageConverter(image);
- return imageByte;
- }
- private static byte[] ImageToByteArraybyImageConverter(System.Drawing.Image image)
- {
- ImageConverter imageConverter = new ImageConverter();
- byte[] imageByte = (byte[])imageConverter.ConvertTo(image, typeof(byte[]));
- return imageByte;
- }
- Convert Image to Byte Array in C# using MemoryStream
- public static byte[] ImageToByteArray(string imagefilePath)
- {
- System.Drawing.Image image = System.Drawing.Image.FromFile(imagefilePath);
- byte[] imageByte = ImageToByteArraybyMemoryStream(image);
- return imageByte;
- }
- private static byte[] ImageToByteArraybyMemoryStream(Image image)
- {
- MemoryStream ms = new MemoryStream();
- image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
- return ms.ToArray();
- }
- WINDOWS FORM CODE
- ======================================================
- // Image Retrive from Folder & Set in PicureBox
- public void GetFilefromDirectory()
- {
- string ss= AppDomain.CurrentDomain.BaseDirectory;
- string[] filePaths = Directory.GetFiles(ss, "*.jpg", SearchOption.AllDirectories);
- if (filePaths.Length > 0)
- {
- pictureBox1.Image = Image.FromFile(filePaths[0].ToString());
- }
- }
- // Image save in location
- public void ImageSaveOnPath()
- {
- //string FilePath = Application.StartupPath + "\\Images\\test2.jpg";
- //pictureBox1.Image.Save(FilePath, ImageFormat.Jpeg);
- SaveFileDialog sfd = new SaveFileDialog();
- sfd.Filter = "Images|*.png;*.bmp;*.jpg";
- ImageFormat format = ImageFormat.Png;
- if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- string ext = System.IO.Path.GetExtension(sfd.FileName);
- switch (ext)
- {
- case ".jpg":
- format = ImageFormat.Jpeg;
- break;
- case ".bmp":
- format = ImageFormat.Bmp;
- break;
- }
- pictureBox1.Image.Save(sfd.FileName, format);
- }
- }
- public void ResizeImagesInFolder(string SourceFolder, string DestinationFolder, int NewImageSize)
- {
- // Check if source folder exists and throw exception if not
- if (!Directory.Exists(SourceFolder))
- throw new Exception("SourceFolder does not exist");
- // Check if destination folder exists, but create it if not
- if (!Directory.Exists(DestinationFolder))
- {
- Directory.CreateDirectory(DestinationFolder);
- }
- // List all images from source directory
- DirectoryInfo diImages = new DirectoryInfo(SourceFolder);
- ArrayList alImages = new ArrayList();
- // GetFiles method doesn't allow us to filter for multiple
- // file extensions, so we must find images in four steps
- // Feel free to add new or remove existing extension to
- // suit your needs
- alImages.AddRange(diImages.GetFiles("*.gif"));
- alImages.AddRange(diImages.GetFiles("*.jpg"));
- alImages.AddRange(diImages.GetFiles("*.bmp"));
- alImages.AddRange(diImages.GetFiles("*.png"));
- Image imgOriginal;
- float OriginalHeight;
- float OriginalWidth;
- int NewWidth;
- int NewHeight;
- Bitmap ResizedBitmap;
- Graphics ResizedImage;
- // Resize every image
- foreach (FileInfo fiImage in alImages)
- {
- // Loads original image from source folder
- imgOriginal = Image.FromFile(fiImage.FullName);
- // Finds height and width of original image
- OriginalHeight = imgOriginal.Height;
- OriginalWidth = imgOriginal.Width;
- // Finds height and width of resized image
- if (OriginalHeight > OriginalWidth)
- {
- NewHeight = NewImageSize;
- NewWidth = (int)((OriginalWidth / OriginalHeight) * (float)NewImageSize);
- }
- else
- {
- NewWidth = NewImageSize;
- NewHeight = (int)((OriginalHeight / OriginalWidth) * (float)NewImageSize);
- }
- // Create new bitmap that will be used for resized image
- ResizedBitmap = new Bitmap(NewWidth, NewHeight);
- ResizedImage = Graphics.FromImage(ResizedBitmap);
- // Resized image will have best possible quality
- ResizedImage.InterpolationMode = InterpolationMode.HighQualityBicubic;
- ResizedImage.CompositingQuality = CompositingQuality.HighQuality;
- ResizedImage.SmoothingMode = SmoothingMode.HighQuality;
- // Draw resized image
- ResizedImage.DrawImage(imgOriginal, 0, 0, NewWidth, NewHeight);
- // Save thumbnail to file
- ResizedBitmap.Save(DestinationFolder + fiImage.Name);
- // It is important to take care of memory, especially in cases
- // when code works with graphics
- imgOriginal.Dispose();
- ResizedBitmap.Dispose();
- ResizedImage.Dispose();
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- GetFilefromDirectory();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- ImageSaveOnPath();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement