Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 30th, 2007, 12:03 AM
Registered User
 
Join Date: May 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default convert Image(jpeg/bmp,etc) file into binary forma

Hi,

 I want to convert Image(jpeg/bmp,etc) file into binary format.and back.. will u please help me to do that.. Its very urgent..

 thanks in advance..

 
Old June 1st, 2007, 01:28 PM
Authorized User
 
Join Date: Feb 2007
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
Default

using System;
using System.Drawing;
using System.IO;

namespace WEImages
{
    /// <summary>
    /// client code recieves the byte stream reprsenting an image and reassembles it
    /// into Bitmap object.Because Bitmap constructor accepts a stream type, we convert
    /// the byte array to memory stream and pass it to the constructor.
    /// </summary>
    public class Client
    {
        public Client()
        {
            // in your case it should be web service proxy object
            WSImages myImage = new WSImages();
            // Request an image from the Web Service
            try
            {
                // call to websevie "GetImage" Web Method
                byte[] image = myImage.GetImage("Csharp.gif"); // or pass specific file id
                MemoryStream memStream = new MemoryStream(image);
                Console.WriteLine(memStream.Length);
                // Convert memory stream to a Bitmap
                Bitmap bm = new Bitmap(memStream);
                // save image returned to local disk(requested server/client machine)
                bm.Save("c:\\ReturnImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (Exception e) // use WebException here to catch web service exception
            {
                Console.WriteLine();
            }
        }


    }
}




using System;
using System.IO;
// add system.Web.Services; System.Web.Services.Protocols;

namespace WEImages
{
    /// <summary>
    /// FileStream is opened and associated with the image file. its contentes are read
    /// into memory and converted to a byte array.The byte array is then sent to the
    /// Web Client(Requested server).
    /// </summary>
    public class WSImages
    {
        public WSImages()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        // Expose this as WebMethod so request server need to pass only file name
        // this is the method which user will able to see for file request
        public byte[] GetImage(string imgFileName)
        {
            byte[] imgArray;
            imgArray = GetBinaryFile("c:\\" + imgFileName + ".gif");
            if (imgArray.Length < 2)
            {
                // use soap Exception here
                throw new Exception("could not open image on server. ");
            }
            else
            {
                return (imgArray);
            }

        }
        // open file to read and reads bytes at a time.you may think to sed chunk at a time
        // to server (client) where they need to convert it again to original file.
        private byte[] GetBinaryFile(string fileName)
        {
            if (File.Exists(fileName))
            {
                try
                {
                    FileStream fs = File.OpenRead(fileName);
                    return ConvertStreamToByeBuffer(fs);
                }
                catch (Exception e)
                {
                    return new byte[0];

                }
            }
            else
            {
                return new byte[0] ;
            }

        }

        // write image to memory as a strem of bytes
        private byte[] ConvertStreamToByeBuffer(FileStream imgStream)
        {
            int imgByte;
            MemoryStream tempStream = new MemoryStream() ;
            while((imgByte= imgStream.ReadByte()) != -1)
            {
                tempStream.WriteByte(((byte)imgByte)) ;
            }
            return tempStream.ToArray(); // convert to array of bytes
        }
    }
}











 
Old September 14th, 2010, 07:58 AM
Registered User
 
Join Date: Sep 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Convert Image into binary

Bitmap bmp = new Bitmap("d:\\temp\\DSC04618.jpg");
MemoryStream mstream = new MemoryStream();
bmp.Save(mstream, ImageFormat.Jpeg);
byte []bytes = new byte[mstream.Length];
mstream.Read(bytes, 0, (int)mstream.Length);

for more details please see http://bimbim.in/post/2010/08/21/How...b-service.aspx

Last edited by bimbim.in; September 14th, 2010 at 07:59 AM.. Reason: speeling correction





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert image to binary form deardp Python 0 September 26th, 2007 11:12 PM
How to convert binary data to image format mshiva Visual Studio 2005 1 February 9th, 2007 10:00 AM
How to draw pic and save as .bmp, .jpn, .jpeg vbadatvb VB How-To 0 March 13th, 2006 01:47 PM
how to convert black & white image into binary? auxiora_nemesis PHP How-To 1 July 27th, 2004 11:09 PM
Read Image and Convert to Binary... soccers_guy10 Pro VB.NET 2002/2003 1 September 22nd, 2003 10:03 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.