Click here to Skip to main content
       

C / C++ / MFC

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page  Show 
QuestionDisplaying Image from streammembertoms from newdelhi24 Feb '13 - 17:55 
I'm trying to display an image from a stream data.
But there is no image when getting image::from stream.
It's my source code:
IStream* pstream = NULL;
	if(SUCCEEDED(CreateStreamOnHGlobal(NULL, TRUE, &pstream)))
	{
		
		ULONG lreal = 0;
 
		pstream->Write(chIncomingDataBuffer, iEnd, &lreal );
		if(pstream!= NULL)
		{
		
		MessageBox(hWnd,
						" Stream is OK",
						"Connection strt",
						MB_ICONINFORMATION|MB_OK);
		
		}
		Image* image =Image::FromStream(pstream);
		if(image)
		{
		
		RECT rect;
 
			::GetWindowRect(hWnd, &rect);
			Graphics graphics(hWnd);
			graphics.DrawImage(image, 0, 0, rect.right-rect.left, rect.bottom-rect.top);
	
		}
		else
		{
		
		MessageBox(hWnd,
						"No image is written",
						"Connection strt",
						MB_ICONINFORMATION|MB_OK);
		
		
		}
		if(image)
			delete image;
		image = NULL;
		if(pstream)
			pstream->Release();
		pstream = NULL;
	
	}
 

There is no image from data. Can anyone help me in this?
Thanks!
AnswerRe: Displaying Image from streammemberSoMad24 Feb '13 - 18:58 
I have never tried doing it this way, but it looks like you took the sample from here[^], removed some stuff to make it simpler and changed it to write directly to your generic IStream object. I am not sure I like the whole thing, but it is kind of an interesting approach - if it works.
 
You do not show how you acquire the data that is stored in chIncomingDataBuffer. Are you sure this buffer contains an entire image?
 
I don't know if it is going to work, but right after you write the data to your pstream, its internal position will be pointing to the end of the data you just wrote. Try calling Seek() in order to change it to point to the start of the data.
 
Soren Madsen
"When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

GeneralRe: Displaying Image from streammembertoms from newdelhi24 Feb '13 - 19:05 
I checked for an image of a particular data bytes then read it and wrote the code like this..
int iff2 = 2970;
	
	iBufferLength = iSpaceRemaining = sizeof(chIncomingDataBuffer);
	iEnd = 0;
	iSpaceRemaining -= iEnd;
 
	iBytesRead = recv(Socket, chIncomingDataBuffer+iEnd, iSpaceRemaining, 0);
	
	iEnd+=iBytesRead;	
	 if(iEnd ==iff2)
	{
	
 

		MessageBox(hWnd,
						"Image  is written ok",
						"Connection strt",
						MB_ICONINFORMATION|MB_OK);
 

	IStream* pstream = NULL;
 

 
In starting if I'm trying to write an image using fwrite then its ok with exact bytes data.
But on stream, its showing some data in stream but not loading any image.
GeneralRe: Displaying Image from streammemberSoMad24 Feb '13 - 19:10 
Ok. Let me know what happens when you call Seek() on pstream after writing the data.
 
Soren Madsen
"When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

GeneralRe: Displaying Image from streammembertoms from newdelhi24 Feb '13 - 19:53 
I added code for Seek() method but problem is same.
ULONG lreal = 0;
		LARGE_INTEGER liBegining = { 0 };
		if(S_OK && pstream)
		{
		pstream->Write(chIncomingDataBuffer, iEnd, &lreal );
		pstream->Seek(liBegining, STREAM_SEEK_SET, NULL);
 
 
this is the added code.
But there is nothing.
GeneralRe: Displaying Image from streammemberSoMad24 Feb '13 - 20:18 
Wait a second. I see that you changed the code. You have a bug in if(S_OK && pstream). The value of S_OK is zero, so in the code above you are not not going to do the Write() and the Seek().
 
Soren Madsen
"When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

GeneralRe: Displaying Image from streammembertoms from newdelhi24 Feb '13 - 20:28 
Yes i also get this., S_OK is zero.
 
But If I'm removing this then pstream is having some value.
Is there any problem with CreateStreamOnHGlobal()?
 
and one more thing,, as I got I can't draw anything outside paint event
if I'm limited to GDI+.
GeneralRe: Displaying Image from streammemberSoMad24 Feb '13 - 21:31 
It seems this is not an easy fix, so let me ask you a couple of things.
- Do you have to use the Image and Graphics stuff?
- What is the format of your images (BMP, JPG, etc.)?
- Are you going to be drawing anything on top of your image?
 
Soren Madsen
"When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

GeneralRe: Displaying Image from streammembertoms from newdelhi24 Feb '13 - 21:53 
-I'll have to recv some data on winsock socket and then display that on screen.
I'm using Image and Graphics it for ease.
-Its BMP
-No nothing on top of my image.
-> I'll have to recv the bytes and display image data on screen without saving it first.
GeneralRe: Displaying Image from streammemberSoMad24 Feb '13 - 22:09 
Good, I figured it was something like this - there are so many classes and tools that need to load the images from file in order to display them, but you have to search hard to find some that will let you load an image already in memory.
 
Take a look at this article: Simple class for drawing pictures (JPG, TIFF, GIF, etc...)[^]
Examine the code to figure out what is wrong with your code or just use the class in your project. I know it is old and there might be newer stuff out there, but I have used the class to perform similar tasks. I have a project where I am using a button class derived from this class and it makes it possible to display just about any image on the buttons.
It has been so long since I looked at the code, that I did not remember it uses a similar approach as you did with the IStream object, so I am sorry I brought that into question.
 
Soren Madsen
"When you don't know what you're doing it's best to do it quickly" - Jase #DuckDynasty

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 23 May 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid