gautampatel15

C# IMAGE COVERT

Aug 8th, 2014
641
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Convert Image File into Byte Array in C#
  2.  
  3. public static byte[] ImageToByteArrayFromFilePath(string imagefilePath)
  4.     {
  5.         byte[] imageArray = File.ReadAllBytes(imagefilePath);
  6.         return imageArray;
  7.     }
  8.  
  9. Convert Image to Byte Array in C# using ImageConverter
  10.  
  11. public static byte[] ImageToByteArray(string imagefilePath)
  12. {
  13.     System.Drawing.Image image = System.Drawing.Image.FromFile(imagefilePath);
  14.     byte[] imageByte = ImageToByteArraybyImageConverter(image);
  15.     return imageByte;
  16. }
  17.  
  18. private static byte[] ImageToByteArraybyImageConverter(System.Drawing.Image image)
  19. {
  20.     ImageConverter imageConverter = new ImageConverter();
  21.     byte[] imageByte = (byte[])imageConverter.ConvertTo(image, typeof(byte[]));
  22.     return imageByte;
  23. }
  24.  
  25. Convert Image to Byte Array in C# using MemoryStream
  26.  
  27. public static byte[] ImageToByteArray(string imagefilePath)
  28. {
  29.     System.Drawing.Image image = System.Drawing.Image.FromFile(imagefilePath);
  30.     byte[] imageByte = ImageToByteArraybyMemoryStream(image);
  31.     return imageByte;
  32. }
  33.  
  34. private static byte[] ImageToByteArraybyMemoryStream(Image image)
  35. {
  36.     MemoryStream ms = new MemoryStream();
  37.     image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  38.     return ms.ToArray();
  39. }
  40.  
  41. WINDOWS FORM CODE
  42. ======================================================
  43.    // Image Retrive from Folder & Set in PicureBox
  44.         public void GetFilefromDirectory()
  45.         {
  46.           string ss=  AppDomain.CurrentDomain.BaseDirectory;
  47.             string[] filePaths = Directory.GetFiles(ss, "*.jpg", SearchOption.AllDirectories);
  48.             if (filePaths.Length > 0)
  49.             {
  50.                 pictureBox1.Image = Image.FromFile(filePaths[0].ToString());
  51.             }
  52.         }
  53.  
  54.  
  55.         // Image save in location
  56.         public void ImageSaveOnPath()
  57.         {
  58.             //string FilePath = Application.StartupPath + "\\Images\\test2.jpg";
  59.             //pictureBox1.Image.Save(FilePath, ImageFormat.Jpeg);
  60.             SaveFileDialog sfd = new SaveFileDialog();
  61.             sfd.Filter = "Images|*.png;*.bmp;*.jpg";
  62.             ImageFormat format = ImageFormat.Png;
  63.             if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  64.             {
  65.                 string ext = System.IO.Path.GetExtension(sfd.FileName);
  66.                 switch (ext)
  67.                 {
  68.                     case ".jpg":
  69.                         format = ImageFormat.Jpeg;
  70.                         break;
  71.                     case ".bmp":
  72.                         format = ImageFormat.Bmp;
  73.                         break;
  74.                 }
  75.                 pictureBox1.Image.Save(sfd.FileName, format);
  76.             }
  77.         }
  78.  
  79.  
  80.         public void ResizeImagesInFolder(string SourceFolder,  string DestinationFolder, int NewImageSize)
  81.         {
  82.             // Check if source folder exists and throw exception if not
  83.             if (!Directory.Exists(SourceFolder))
  84.                 throw new Exception("SourceFolder does not exist");
  85.  
  86.             // Check if destination folder exists, but create it if not
  87.             if (!Directory.Exists(DestinationFolder))
  88.             {
  89.                 Directory.CreateDirectory(DestinationFolder);
  90.             }
  91.  
  92.             // List all images from source directory
  93.             DirectoryInfo diImages = new DirectoryInfo(SourceFolder);
  94.             ArrayList alImages = new ArrayList();
  95.             // GetFiles method doesn't allow us to filter for multiple
  96.            // file extensions, so we must find images in four steps
  97.            // Feel free to add new or remove existing extension to
  98.            // suit your needs
  99.            alImages.AddRange(diImages.GetFiles("*.gif"));
  100.            alImages.AddRange(diImages.GetFiles("*.jpg"));
  101.            alImages.AddRange(diImages.GetFiles("*.bmp"));
  102.            alImages.AddRange(diImages.GetFiles("*.png"));
  103.  
  104.            Image imgOriginal;
  105.            float OriginalHeight;
  106.            float OriginalWidth;
  107.            int NewWidth;
  108.            int NewHeight;
  109.            Bitmap ResizedBitmap;
  110.            Graphics ResizedImage;
  111.  
  112.            // Resize every image
  113.            foreach (FileInfo fiImage in alImages)
  114.            {
  115.                // Loads original image from source folder
  116.                imgOriginal = Image.FromFile(fiImage.FullName);
  117.                // Finds height and width of original image
  118.                OriginalHeight = imgOriginal.Height;
  119.                OriginalWidth = imgOriginal.Width;
  120.                // Finds height and width of resized image
  121.                if (OriginalHeight > OriginalWidth)
  122.                {
  123.                    NewHeight = NewImageSize;
  124.                    NewWidth = (int)((OriginalWidth / OriginalHeight) * (float)NewImageSize);
  125.                }
  126.                else
  127.                {
  128.                    NewWidth = NewImageSize;
  129.                    NewHeight = (int)((OriginalHeight / OriginalWidth) * (float)NewImageSize);
  130.                }
  131.                // Create new bitmap that will be used for resized image
  132.                ResizedBitmap = new Bitmap(NewWidth, NewHeight);
  133.                ResizedImage = Graphics.FromImage(ResizedBitmap);
  134.                // Resized image will have best possible quality
  135.                ResizedImage.InterpolationMode = InterpolationMode.HighQualityBicubic;
  136.                ResizedImage.CompositingQuality = CompositingQuality.HighQuality;
  137.                ResizedImage.SmoothingMode = SmoothingMode.HighQuality;
  138.                // Draw resized image
  139.                ResizedImage.DrawImage(imgOriginal, 0, 0, NewWidth, NewHeight);
  140.                // Save thumbnail to file
  141.                ResizedBitmap.Save(DestinationFolder + fiImage.Name);
  142.  
  143.                // It is important to take care of memory, especially in cases
  144.                // when code works with graphics
  145.                imgOriginal.Dispose();
  146.                ResizedBitmap.Dispose();
  147.                ResizedImage.Dispose();
  148.            }
  149.        }
  150.  
  151.        private void button1_Click(object sender, EventArgs e)
  152.        {
  153.            GetFilefromDirectory();
  154.        }
  155.  
  156.        private void button2_Click(object sender, EventArgs e)
  157.        {
  158.            ImageSaveOnPath();
  159.        }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×