Sunday, February 5, 2012

How to convert an image to bytes in .NET?

One of the most standard queries I come across in many of the coding forums these days is, how do we convert an image (Image/Bitmap class) in .NET to a byte[] array? The answer to this is very straight forward - a few lines of code in VB.NET or C#:



VB.NET:

        Private Function CaptureScreen() As Bitmap
            'CAPTURE THE BITMAP
            ' Size size is how big an area to capture
            ' pointOrigin is the upper left corner of the area to capture
            Dim width As Integer =  Screen.PrimaryScreen.Bounds.X + Screen.PrimaryScreen.Bounds.Width
            Dim height As Integer =  Screen.PrimaryScreen.Bounds.Y + Screen.PrimaryScreen.Bounds.Height
            Dim size As Size =  New Size(width,height)
            Dim pointOfOrigin As Point =  New Point(0,0)
            Dim desktopBitmap As Bitmap =  New Bitmap(size.Width,size.Height)
            Imports (Graphics graphics = Graphics.FromImage(desktopBitmap))
            {
                graphics.CopyFromScreen(pointOfOrigin,New Point(0,0),size)
            }
            'CAPTURE CURSOR
            Dim cursorX As Integer = 0,cursorY As Integer =  0
            Dim cursorBitmap As Bitmap =  CaptureCursor( cursorX,ref cursorY)
            'MERGE THE CURSOR INTO DESKTOP BITMAP
            If Not cursorBitmap Is Nothing Then
                Dim g As Graphics
                Dim r As Rectangle
                r = New Rectangle(cursorX, cursorY, cursorBitmap.Width, cursorBitmap.Height)
                g = Graphics.FromImage(desktopBitmap)
                g.DrawImage(cursorBitmap, r)
                g.Flush()
                Return desktopBitmap
            Else
                Return desktopBitmap
            End If
        End Function


C#: 


        private Bitmap CaptureScreen()
        {
            //CAPTURE THE BITMAP
            // Size size is how big an area to capture
            // pointOrigin is the upper left corner of the area to capture
            int width = Screen.PrimaryScreen.Bounds.X + Screen.PrimaryScreen.Bounds.Width;
            int height = Screen.PrimaryScreen.Bounds.Y + Screen.PrimaryScreen.Bounds.Height;
            Size size = new Size(width, height);
            Point pointOfOrigin = new Point(0, 0);
            Bitmap desktopBitmap = new Bitmap(size.Width, size.Height);
            using (Graphics graphics = Graphics.FromImage(desktopBitmap))
            {
                graphics.CopyFromScreen(pointOfOrigin, new Point(0, 0), size);
            }
            //CAPTURE CURSOR
            int cursorX=0, cursorY=0;
            Bitmap cursorBitmap = CaptureCursor(ref cursorX,ref cursorY);
            //MERGE THE CURSOR INTO DESKTOP BITMAP
            if (cursorBitmap != null)
            {
                Graphics g;
                Rectangle r;
                r = new Rectangle(cursorX, cursorY, cursorBitmap.Width, cursorBitmap.Height);
                g = Graphics.FromImage(desktopBitmap);
                g.DrawImage(cursorBitmap, r);
                g.Flush();
                return desktopBitmap;
            }
            else
            {
                return desktopBitmap;
            }
        }




Do note that the parameter passed to this function should be an object of System.Drawing.Bitmap type. If you have an object of Image type instead, you may convert it to Bitmap by doing this:


Graphics.FromImage(imageObject)

2 comments:

  1. thanks for sharing, to convert image to btyes ,you can try this image converter
    http://www.rasteredge.com/how-to/csharp-imaging/convert-image-to-byte/

    ReplyDelete