Convert Image To Bytes - CSharp System.Drawing

CSharp examples for System.Drawing:Image Convert

Description

Convert Image To Bytes

Demo Code


using System.Windows.Forms;
using System.Text;
using System.IO;//from ww  w.jav  a 2s.co m
using System.Drawing.Imaging;
using System.Drawing;
using System;

public class Main{
        public static byte[] ConvertToBytes(Image image, float quality)
        {
            if (quality < 0.99f)
            {
                int thumbWidth = (int)(image.Width * quality);
                int thumbHeight = (int)(image.Height * quality);
                Image thumb = image.GetThumbnailImage(thumbWidth, thumbHeight, null, IntPtr.Zero);
                MemoryStream stream = new MemoryStream();
                thumb.Save(stream, ImageFormat.Jpeg);
                byte[] imageData = stream.ToArray();
                stream.Close();
                thumb.Dispose();
                return imageData;
            }
            else
            {
                MemoryStream stream = new MemoryStream();
                image.Save(stream, ImageFormat.Jpeg);
                byte[] imageData = stream.ToArray();
                stream.Close();
                return imageData;
            }
        }
}

Related Tutorials