Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

saving images to streams

Managed C++ form stream

  • Please log in to reply
1 reply to this topic

#1 Darkkender

Darkkender

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 23 May 2007 - 12:58 PM

I've been having some difficulty saving an image file within a stream with other data. The current application Form I have been working on has an image file plus a variety of other data. I'm able to write all of the other data too the stream I have been meeting with a big hurdle of trying to save the image file into the same binary stream as the rest of the data. I'll also need to be able to retrieve the image data from the stream and load it back into the app form. If anybody could help with this it would be apreciated.
  • 0

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 24 April 2012 - 09:01 PM

The trick lies in saving the length of the images first on the stream followed by image data itself on the binary stream. Do the same while reading back. Following is the working sample as a solution to your problem.


void ImageWriteToBinaryFile()
{
// First open the binary file we want to write to binary data.
System::IO::FileStream^ binaryStream = System::IO::File::Open("C:\\binary-image.bin", 
System::IO::FileMode::Create);
System::IO::BinaryWriter^ writer = gcnew  System::IO::BinaryWriter(binaryStream);


// write some data other than images.
writer->Write("String data"); // string data
writer->Write((int)(100)); // integer data

// Read the image file you want to save to as binary data
System::IO::FileStream^ imageStream = System::IO::File::OpenRead("C:\\image.jpg");

// IMPORTANT: write the length of the image data to binary stream so that we can detect the size
// of the image when we will read later.
writer->Write(imageStream->Length); 

// Read the data from the image-stream as byte array
array<unsigned char>^ imageData = gcnew array<unsigned char>(imageStream->Length);
imageStream->Read(imageData, 0, imageData->Length);

// write the image data to binary stream
writer->Write(imageData);

imageStream->Close();
binaryStream->Close();
}




void ImageReadFromBinaryFile()
{
// First open the binary file we want to read binary data.
System::IO::FileStream^ stream = System::IO::File::Open("C:\\binary-image.bin",
System::IO::FileMode::Open);
System::IO::BinaryReader^ reader = gcnew  System::IO::BinaryReader(stream);


// read some data other than images which we wrote in ImageWrite method.
System::String^ sData = reader->ReadString(); // reading string
int iData = reader->ReadInt32();

// Read the image image. Note that we wrote 8 bytes lenght integer which is 
// the size of the image data we wrote in as binary.
unsigned __int64 imageDataLength = reader->ReadUInt64();
array<unsigned char>^ imageData = reader->ReadBytes(imageDataLength);

// Create a memory stream of the bytes we just read from stream
System::IO::MemoryStream^ imageStream = gcnew System::IO::MemoryStream(imageData);

// Use FromStream static method to get the image from the memory stream.
Image^ img = Image::FromStream(imageStream);

// Save the images just as a proof
img->Save("C:\\image-out.jpg", System::Drawing::Imaging::ImageFormat::Jpeg);

stream->Close();

// we are setting the image as background of the form -- as a proof to see alive.
this->BackgroundImage = img;
}

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download