Thread: Displaying Image from stream

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    15

    Displaying Image from stream

    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:
    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.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Please indent your code correctly and follow a well-accepted coding style such as Allman.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    15
    Ok...

    Code:
    IStream* pstream = NULL;
        if(SUCCEEDED(CreateStreamOnHGlobal(NULL, TRUE, &pstream)))
        {
            
            ULONG lreal = 0;
            if(S_OK)
            {
            pstream->Write(chIncomingDataBuffer, iEnd, &lreal );
            
            }
            else
            {
            
                MessageBox(hWnd,
                            "No Stream is written",
                            "Connection strt",
                            MB_ICONINFORMATION|MB_OK);
            }
            
            if(pstream!= NULL)
            {
            
            MessageBox(hWnd,
                            " Stream is written",
                            "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);
        
            }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > pstream->Write(chIncomingDataBuffer, iEnd, &lreal );
    ...
    > Image* image =Image::FromStream(pstream);
    OK, what is the point of writing to a stream and immediately trying to read from it?
    Or even, what is the point of writing to a stream which is an Input stream to begin with?


    > if(image)
    Where is your error popup telling you what might be wrong, if this is false?


    > if(pstream!= NULL)
    It's too late for this, you already used pstream->Write
    Perhaps you're trying to check the status of the previous write, in which case, this isn't doing it for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    if (S_OK) makes no sense.
    S_OK is defined as zero, so you're saying "if (0)", which is always false.
    I see no code to call Release on the IStream either.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    15
    I took the reference from these two topics and started writing my own code-
    CreateStreamOnHGlobal function (Windows)
    Image.FromStream method (Windows)

    and on S_OK the value is zero on debugging, so it's been removed.
    on global allocating and on CreateStreamOnHGlobal-Istream is having some value.
    That value should be declared to an image.
    If I'm not drawing the image(Graphics only works on WM_PAINT), then it will load the image from stream.
    Code:
    Image*image = Image::FromStream(pstream);
                
            if(image)
            {
            MessageBox(hWnd,
                            " Image is there",
                            "Connection strt",
                            MB_ICONINFORMATION|MB_OK);
            
            
            }
            else
            {
            
            MessageBox(hWnd,
                            "no Image is there",
                            "Connection strt",
                            MB_ICONINFORMATION|MB_OK);
            
            
            }
            pstream->Release();
    it's always null value on image.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP!!!!!Image encryption using DCT and Stream Cipher
    By abujdan in forum C Programming
    Replies: 4
    Last Post: 03-16-2011, 10:54 AM
  2. Displaying an Image
    By evanman in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2006, 01:04 PM
  3. Displaying a PNG image
    By grep in forum Windows Programming
    Replies: 5
    Last Post: 07-07-2005, 08:27 PM
  4. Image displaying
    By homeyg in forum C++ Programming
    Replies: 16
    Last Post: 01-03-2005, 06:27 PM