Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Closed Thread
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 30th, 2007, 12:03 AM
Registered User
 
Join Date: May 2007
Location: Hyderabad, AndhraPradesh, India.
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..

  #2 (permalink)  
Old June 1st, 2007, 01:28 PM
Authorized User
 
Join Date: Feb 2007
Location: , , USA.
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
        }
    }
}











  #3 (permalink)  
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
Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:37 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.