Register and join over 40,000 other developers!
Recent Topics
-
The Game You Are Waiting For?
WendellHarper - Dec 06 2020 01:21 PM
-
Quora and Reddit Backlinks
WendellHarper - Dec 06 2020 01:14 PM
-
Delete account
pindo - Jul 23 2020 01:33 AM
-
Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
-
Learn algorithms and programming concepts
johnnylo - Apr 23 2019 07:49 AM
Recent Blog Entries
Recent Status Updates
Popular Tags
- networking
- Managed C++
- stream
- console
- database
- authentication
- Visual Basic 4 / 5 / 6
- session
- Connection
- asp.net
- import
- syntax
- hardware
- html5
- array
- mysql
- java
- php
- c++
- string
- C#
- html
- loop
- timer
- jquery
- ajax
- javascript
- programming
- android
- css
- assembly
- c
- form
- vb.net
- xml
- linked list
- login
- encryption
- pseudocode
- calculator
- sql
- python
- setup
- help
- game
- combobox
- binary
- hello world
- grid
- innerHTML

1 reply to this topic
#1
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.
#2
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; }
Also tagged with one or more of these keywords: Managed C++, form, stream
General Forums →
General Programming →
Specifying PHP file path while creating an HTML formStarted by gvvishwanath, 30 Dec 2015 ![]() |
|
![]() |
||
Language Forums →
PHP →
Save JPG image every second - but avoid saving damaged/corrupt images comprised of multiple images?Started by elliottveares, 24 Nov 2015 ![]() |
|
![]() |
||
Language Forums →
HTML, CSS and Javascript →
Jquery Validation in Partial View Form Isn't TriggeringStarted by googaplex, 01 Jul 2015 ![]() |
|
![]() |
||
Language Forums →
C# →
(C#) How to capture video stream without using a third party SDKStarted by BaumHard920, 17 Nov 2014 ![]() |
|
![]() |
||
Language Forums →
HTML, CSS and Javascript →
how to deal with the forms indtified with idStarted by scseguanshui06, 20 Jul 2014 ![]() |
|
![]() |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download