【问题标题】:libJPEG WinAPI ErrorlibJPEG WinAPI 错误
【发布时间】:2015-11-18 10:56:21
【问题描述】:

程序读取JPEG文件

void read_jpeg_file(char *szNamaFile)
{
    size_t i;
    unsigned char* raw_image;
    JSAMPROW row_pointer[1];
    unsigned long location = 0;

    struct jpeg_error_mgr jerr;
    struct jpeg_decompress_struct cinfo ;

    errno_t err;
    FILE *infile;
    err = fopen_s(&infile, szNamaFile, "rb" );

    if (infile == NULL )
    {
        printf("Error opening jpeg file %s\n!", szNamaFile );
        exit(EXIT_FAILURE);
    }
    cinfo.err = jpeg_std_error(&jerr);

    /* create decompressor */
    jpeg_create_decompress(&cinfo);

    /* this makes the library read from infile */
    jpeg_stdio_src(&cinfo, infile );

    /* read jpeg header */
    jpeg_read_header(&cinfo, TRUE);

    /* decompress */
    jpeg_start_decompress(&cinfo);

    /*allocate memory */
    raw_image = (unsigned char*)malloc( cinfo.output_width*cinfo.output_height*cinfo.num_components );

    /* now actually read the jpeg into the raw buffer */
    row_pointer[0] = (unsigned char *)malloc( cinfo.output_width*cinfo.num_components );
    /* read scanlines */
    while (cinfo.output_scanline < cinfo.output_height) {
        jpeg_read_scanlines( &cinfo, row_pointer, 1 );
        for(i = 0; i < cinfo.image_width*cinfo.num_components; i++) 
            raw_image[location++] = row_pointer[0][i];
    }  

    /* clean up */
    jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);
    fclose( infile );
    free( row_pointer[0] );
}

过程打开文件对话框

void OpenDialog(HWND hWnd) 
{
    OPENFILENAME    ofn     = {sizeof(ofn)};

    ofn.hwndOwner       = hWnd;
    ofn.lpstrFilter     = TEXT("File BMP (*.BMP)\0*.BMP\0File JPG (*.JPG)\0*.JPG\0");
    ofn.nFilterIndex    = 1;
    ofn.lpstrFile       = szNamaFile;
    ofn.lpstrFile[0]    = '\0';
    ofn.lpstrTitle      = TEXT("Test ...");
    ofn.nMaxFile        = sizeof(szNamaFile);
    ofn.Flags           = OFN_FILEMUSTEXIST;

  if(GetOpenFileName(&ofn))
  {
      read_jpeg_file(ofn.lpstrFile);
  }
}

WM_COMMAND

case WM_COMMAND:
        if(HIWORD(wParam) != BN_CLICKED)
            break;
        switch(LOWORD(wParam))
        {
            case IDB_LOAD:
                OpenDialog(hWnd);
                break;
        }
        break;

图片不显示,如何解决?也许我应该向 WM_PAINT 发送消息?
对不起,如果我问了很多问题,因为我还是初学者
谢谢你,对不起我的英语不好

【问题讨论】:

  • 另外,考虑到您可以在 XP 中打开 BMP/GIF/JPEG/PNG/TIFF/Exif/WMF/EMF,只使用 Windows 本身的组件 - 不需要外部库。您可以使用 GDI+ 来完成,使用 Gdiplus::Bitmap 类。初始化 GDI+ 后,您可以在 GdiPlus::Bitmap 的构造函数中抛出 WCHAR * 和 BOOL,然后使用 GetHBITMAP 成员函数从它获取 HBITMAP。我的评论比执行上述任何/所有图像格式的任务所需的代码长!
  • 感谢您的建议,但我想知道如何使用库 libjpeg

标签: image winapi libjpeg


【解决方案1】:

要修复第一个警告,请像这样使用fopen_s

FILE *infile;
errno_t err = fopen_s(infile, szNamaFile, "rb");
if(0 == err)
{
    // Proceed with reading if file is opened properly
    ...
}

要修复第二个警告,请将int 替换为size_t

size_t i;
for( i = 0; i < cinfo.image_width * cinfo.num_components; i++)

【讨论】:

  • 谢谢我已经修复了错误,但是为什么图片没有出现?
  • @RyuAndyaPratama 我无法知道这一点,您没有发布任何处理图像绘制的代码。顺便说一句,我同意@enhzflep 在评论部分提出的建议。如果您没有使用 libJPEG-Turbo 的特定原因,那么使用 Gdiplus::Bitmap 类会容易得多。这样,您就可以轻松读取和绘制图像并同时支持多种格式。
  • 您在您的交易中发布的代码用于打开一个对话框,该对话框用于选择磁盘上的图像文件。就绘图和图像而言,我记得的唯一方法是使用raw_image 中的像素数据创建一个Gdiplus::Bitmap 对象,然后在处理窗口的WM_PAINT 消息时使用Gdiplus::Graphics::DrawImage 绘制它。转换为Bitmap 是一项相当复杂的任务,有关如何使用此类的文档,请参阅 MSDN。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-23
  • 2021-04-04
  • 2016-02-10
相关资源
最近更新 更多