Downloading and displaying a jpg file

This is a discussion on Downloading and displaying a jpg file within the Windows Programming forums, part of the Platform Specific Boards category; I want to download a jpg file from internet, but I want to make the header up myself and download ...

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Downloading and displaying a jpg file

    I want to download a jpg file from internet, but I want to make the header up myself and download it using WinSock. I know how to use sockets and how to make POST queries etc. My question is: How do I receive a jpg from server so the file remains untouched and doesn't break on 0 byte, and then how do I display it from the memory?
    There must be some libraries that allow you to load jpg from memory, not from a file...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    What do you mean make the header up myself? The HTTP header?

    The libgd allows to load it from in-memory:

    Code:
    gdImageCreateFromJpeg(FILE *in) (FUNCTION)
    gdImageCreateFromJpegPtr(int size, void *data) (FUNCTION)
    gdImageCreateFromJpegCtx(gdIOCtx *in) (FUNCTION)
    More docs and suchlike. http://www.boutell.com/gd/manual2.0.33.html

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Found this:
    http://www.codeguru.com/forum/showth...46#post1496946

    I will try libgd then.

    Yes, the HTTP header, I need to add a cookie line there...
    Last edited by maxorator; 11-30-2006 at 04:11 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    That is poorly written code, prone to failure.

    /SNES Starfox Announcer/ Good Luck /SNES Starfox Announcer/

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Now what is the best way to get pieces of binary data that are held in char arrays together?
    Use strncat(), so everything after the 0 bytes will be copied too?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    How do I display the JPG file with libgd after making it an image? I thought maybe you know, so I don't have to mess with it so much.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Who said you're recieving null-terminated strings from the socket? And no, strncat won't do that. It will stop at the null and zero-pad the strlen(src) + (maxlen - strlen(src)) of the dest.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    So I'll copy it byte by byte?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    It won't display your picture. You can use some COM to do that relatively easily.

    IStream -> Write() yr JPG data
    IPicture -> OleLoadPicture() <- IStream -> Render()

    http://msdn.microsoft.com/msdnmag/issues/01/10/c/

    >> So I'll copy it byte by byte?

    Whatru talkin' bout? Don't use it. Use memory management routines i.e. memcpy

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    So I'll copy it byte by byte?
    if you need to copy several bytes that are not a string you can use memmove or memcpy
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Does libgd make it into a bitmap? So I can display it as a normal resource image?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  12. #12
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    No.

    (Your message is too short. Please lengthen it to at least 4 characters)

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    What does it convert it to then?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  14. #14
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Okay you may be able to I spose, I can't tell what MSDN docs say about the pixel data being word aligned but I spose you could format like that if ya needed to. CreateBitmap plus this stuff;

    Code:
    gdImage(TYPE)
        The data structure in which gd stores images. gdImageCreate, gdImageCreateTrueColor and the various image file-loading functions return a pointer to this type, and the other functions expect to receive a pointer to this type as their first argument. It is reasonably safe to examine any of the members of this structure. It is also reasonably safe to modify individual pixels within the pixels or tpixels arrays. If the trueColor flag is set, the tpixels array is valid; otherwise the pixels array is valid.
    
        The colorsTotal, red, green, blue, alpha and open arrays manage the palette. They are valid only when the trueColor flag is not set. The transparent value contains the palette index of the first transparent color as read-only information for backwards compatibility; gd 2.0 stores this information in the alpha array so that variable transparency can be supported for each palette entry. However, for truecolor images, transparent represents a single RGB color which is always 100% transparent, and this feature is generally supported by browsers which do not support full alpha channels.
    
        typedef struct {
          /* Palette-based image pixels */
          unsigned char ** pixels;
          int sx;
          int sy;
          /* These are valid in palette images only. See also
          /* 'alpha', which appears later in the structure to
            preserve binary backwards compatibility */
          int colorsTotal;
          int red[gdMaxColors];
          int green[gdMaxColors];
          int blue[gdMaxColors]; 
          int open[gdMaxColors];
          /* For backwards compatibility, this is set to the
            first palette entry with 100% transparency,
            and is also set and reset by the 
            gdImageColorTransparent function. Newer
            applications can allocate palette entries
            with any desired level of transparency; however,
            bear in mind that many viewers, notably
            many web browsers, fail to implement
            full alpha channel for PNG and provide
            support for full opacity or transparency only. */
          int transparent;
          int *polyInts;
          int polyAllocated;
          struct gdImageStruct *brush;
          struct gdImageStruct *tile;  
          int brushColorMap[gdMaxColors];
          int tileColorMap[gdMaxColors];
          int styleLength;
          int stylePos;
          int *style;
          int interlace;
          /* New in 2.0: alpha channel for palettes. Note that only
            Macintosh Internet Explorer and (possibly) Netscape 6
            really support multiple levels of transparency in
            palettes, to my knowledge, as of 2/15/01. Most
            common browsers will display 100% opaque and
            100% transparent correctly, and do something 
            unpredictable and/or undesirable for levels
            in between. TBB */
          int alpha[gdMaxColors]; 
          /* Truecolor flag and pixels. New 2.0 fields appear here at the
            end to minimize breakage of existing object code. */
          int trueColor;
          int ** tpixels;
          /* Should alpha channel be copied, or applied, each time a
            pixel is drawn? This applies to truecolor images only.
            No attempt is made to alpha-blend in palette images,
            even if semitransparent palette entries exist. 
            To do that, build your image as a truecolor image,
            then quantize down to 8 bits. */
          int alphaBlendingFlag;
          /* Should the alpha channel of the image be saved? This affects
            PNG at the moment; other future formats may also
            have that capability. JPEG doesn't. */
          int saveAlphaFlag;
        } gdImage;
    
        The order of the structure members may appear confusing, but was chosen deliberately to increase backwards compatibility with existing gd 1.x-based binary code that references particular structure members.
    If you get this working, I'd be interested in the solution. I could see it being really really easy if CreateBitmap just accepts that pixel format of the gdImage.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21