求助大神,如何用c语言编写二进制字节流转换成jpg格式图片?

lyc35852 2016-06-28 09:24:19
如题,如何用c语言把字节流转换成jpg格式图片?能调节像素多少和输出路径,最近想实现下360在深度学习的流量识别,将数据流转换成jpg格式图片没什么思路,求问如何实现?
...全文
902 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyc35852 2016-06-28
  • 打赏
  • 举报
回复
引用 1 楼 赵4老师的回复:
仅供参考:
#pragma comment(lib,"user32")
#pragma comment(lib,"gdi32")
#include <stdlib.h>
#include <stdio.h>
#include <io.h>
#include <conio.h>
#include <windows.h>
#include <atlimage.h>
#include <objidl.h>
void DrawPic(HDC hdc,char *buf,int len) {
    HGLOBAL hMem=GlobalAlloc(GMEM_FIXED,len);
    BYTE* pMem=(BYTE*)GlobalLock(hMem);
    memcpy(pMem,buf,len);
    IStream* pStream;
    HRESULT hr=CreateStreamOnHGlobal(pMem,FALSE,&pStream);

    CImage img;
    img.Load(pStream);
    img.Draw(hdc,CPoint(0,0));

    img.Destroy();
    pStream->Release();
    GlobalUnlock(hMem);
    GlobalFree(hMem);
}
//HWND WINAPI GetConsoleWindow();
void HideTheCursor() {
    CONSOLE_CURSOR_INFO cciCursor;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (GetConsoleCursorInfo(hStdOut, &cciCursor)) {
        cciCursor.bVisible = FALSE;
        SetConsoleCursorInfo(hStdOut, &cciCursor);
    }
}
void ShowTheCursor() {
    CONSOLE_CURSOR_INFO cciCursor;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (GetConsoleCursorInfo(hStdOut, &cciCursor)) {
        cciCursor.bVisible = TRUE;
        SetConsoleCursorInfo(hStdOut, &cciCursor);
    }
}
int main() {
    HWND  hwnd;
    HDC   hdc;
    HFONT hfont;
    HBITMAP hbm;
    HDC hdcBits;
    BITMAP bm;

    system("color F0");
    system("cls");
    HideTheCursor();
    hwnd  = GetConsoleWindow();
    hdc   = GetDC(hwnd);
    hfont = CreateFont(48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "华文楷体");
    SelectObject(hdc,hfont);
    TextOut(hdc,10,10,"这是泡泡",8);
    DeleteObject(hfont);
    hbm=(HBITMAP)LoadImage(0,"C:\\Windows\\Soap Bubbles.bmp",IMAGE_BITMAP,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);
    if (hbm) {
        hdcBits = CreateCompatibleDC(hdc);
        GetObject (hbm, sizeof(BITMAP), &bm);
        SelectObject(hdcBits,hbm);
        BitBlt(hdc,200,10,bm.bmWidth, bm.bmHeight,hdcBits,0,0,SRCCOPY);
        DeleteDC(hdcBits);
        DeleteObject(hbm);
    }
    getch();
    FILE *f;
    f=fopen("c:\\new\\tmp.jpg","rb");
    if (f) {
        int fl=filelength(fileno(f));
        char *buf=(char *)malloc(fl);
        if (buf) {
            fread(buf,fl,1,f);
        }
        fclose(f);
        if (buf) {
            DrawPic(hdc,buf,fl);
            free(buf);
        }
    }
    ReleaseDC(hwnd,hdc);
    getch();
    system("color 07");
    system("cls");
    ShowTheCursor();
    return 0;
}
谢谢老师,我试试看看
赵4老师 2016-06-28
  • 打赏
  • 举报
回复
仅供参考:
#pragma comment(lib,"user32")
#pragma comment(lib,"gdi32")
#include <stdlib.h>
#include <stdio.h>
#include <io.h>
#include <conio.h>
#include <windows.h>
#include <atlimage.h>
#include <objidl.h>
void DrawPic(HDC hdc,char *buf,int len) {
    HGLOBAL hMem=GlobalAlloc(GMEM_FIXED,len);
    BYTE* pMem=(BYTE*)GlobalLock(hMem);
    memcpy(pMem,buf,len);
    IStream* pStream;
    HRESULT hr=CreateStreamOnHGlobal(pMem,FALSE,&pStream);

    CImage img;
    img.Load(pStream);
    img.Draw(hdc,CPoint(0,0));

    img.Destroy();
    pStream->Release();
    GlobalUnlock(hMem);
    GlobalFree(hMem);
}
//HWND WINAPI GetConsoleWindow();
void HideTheCursor() {
    CONSOLE_CURSOR_INFO cciCursor;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (GetConsoleCursorInfo(hStdOut, &cciCursor)) {
        cciCursor.bVisible = FALSE;
        SetConsoleCursorInfo(hStdOut, &cciCursor);
    }
}
void ShowTheCursor() {
    CONSOLE_CURSOR_INFO cciCursor;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (GetConsoleCursorInfo(hStdOut, &cciCursor)) {
        cciCursor.bVisible = TRUE;
        SetConsoleCursorInfo(hStdOut, &cciCursor);
    }
}
int main() {
    HWND  hwnd;
    HDC   hdc;
    HFONT hfont;
    HBITMAP hbm;
    HDC hdcBits;
    BITMAP bm;

    system("color F0");
    system("cls");
    HideTheCursor();
    hwnd  = GetConsoleWindow();
    hdc   = GetDC(hwnd);
    hfont = CreateFont(48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "华文楷体");
    SelectObject(hdc,hfont);
    TextOut(hdc,10,10,"这是泡泡",8);
    DeleteObject(hfont);
    hbm=(HBITMAP)LoadImage(0,"C:\\Windows\\Soap Bubbles.bmp",IMAGE_BITMAP,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);
    if (hbm) {
        hdcBits = CreateCompatibleDC(hdc);
        GetObject (hbm, sizeof(BITMAP), &bm);
        SelectObject(hdcBits,hbm);
        BitBlt(hdc,200,10,bm.bmWidth, bm.bmHeight,hdcBits,0,0,SRCCOPY);
        DeleteDC(hdcBits);
        DeleteObject(hbm);
    }
    getch();
    FILE *f;
    f=fopen("c:\\new\\tmp.jpg","rb");
    if (f) {
        int fl=filelength(fileno(f));
        char *buf=(char *)malloc(fl);
        if (buf) {
            fread(buf,fl,1,f);
        }
        fclose(f);
        if (buf) {
            DrawPic(hdc,buf,fl);
            free(buf);
        }
    }
    ReleaseDC(hwnd,hdc);
    getch();
    system("color 07");
    system("cls");
    ShowTheCursor();
    return 0;
}

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧