2014-03-28 65 views
0

我想要一個bmp圖像轉換成PNG一個與此代碼:錯誤:LNK1120:9周解析的外部

#define WIN32_LEAN_AND_MEAN 
#define _CRT_SECURE_NO_DEPRECATE 

#include <png.h> 
#include <stdlib.h> 
#include <stdio.h> 
#include <stdint.h> 

void GetDesktopResolution(int& horizontal, int& vertical) 
{ 
    RECT desktop; 
    // Get a handle to the desktop window 
    const HWND hDesktop = GetDesktopWindow(); 
    // Get the size of screen to the variable desktop 
    GetWindowRect(hDesktop, &desktop); 
    // The top left corner will have coordinates (0,0) 
    // and the bottom right corner will have coordinates 
    // (horizontal, vertical) 
    horizontal = desktop.right; 
    vertical = desktop.bottom; 
} 

typedef struct _RGBPixel { 
    uint8_t blue; 
    uint8_t green; 
    uint8_t red; 
} RGBPixel; 

/* Structure for containing decompressed bitmaps. */ 
typedef struct _RGBBitmap { 
    RGBPixel *pixels; 
    size_t width; 
    size_t height; 
    size_t bytewidth; 
    uint8_t bytes_per_pixel; 
} RGBBitmap; 

/* Returns pixel of bitmap at given point. */ 
#define RGBPixelAtPoint(image, x, y) \ 
    *(((image)->pixels) + (((image)->bytewidth * (y)) \ 
         + ((x) * (image)->bytes_per_pixel))) 

/* Attempts to save PNG to file; returns 0 on success, non-zero on error. */ 
int save_png_to_file(RGBBitmap *bitmap, const char *path) 
{ 
    FILE *fp = fopen(path, "wb"); 
    png_structp png_ptr = NULL; 
    png_infop info_ptr = NULL; 
    size_t x, y; 
    png_uint_32 bytes_per_row; 
    png_byte **row_pointers = NULL; 

    if (fp == NULL) return -1; 

    /* Initialize the write struct. */ 
    png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); 
    if (png_ptr == NULL) { 
     fclose(fp); 
     return -1; 
    } 

    /* Initialize the info struct. */ 
    info_ptr = png_create_info_struct(png_ptr); 
    if (info_ptr == NULL) { 
     png_destroy_write_struct(&png_ptr, NULL); 
     fclose(fp); 
     return -1; 
    } 

    /* Set up error handling. */ 
    if (setjmp(png_jmpbuf(png_ptr))) { 
     png_destroy_write_struct(&png_ptr, &info_ptr); 
     fclose(fp); 
     return -1; 
    } 

    /* Set image attributes. */ 
    png_set_IHDR(png_ptr, 
       info_ptr, 
       bitmap->width, 
       bitmap->height, 
       8, 
       PNG_COLOR_TYPE_RGB, 
       PNG_INTERLACE_NONE, 
       PNG_COMPRESSION_TYPE_DEFAULT, 
       PNG_FILTER_TYPE_DEFAULT); 

    /* Initialize rows of PNG. */ 
    bytes_per_row = bitmap->width * bitmap->bytes_per_pixel; 
    png_malloc(png_ptr, bitmap->height * sizeof(png_byte *)); 
    for (y = 0; y < bitmap->height; ++y) { 
     uint8_t *row = (uint8_t *)png_malloc(png_ptr, sizeof(uint8_t)* bitmap->bytes_per_pixel); 
     row_pointers[y] = (png_byte *)row; 
     for (x = 0; x < bitmap->width; ++x) { 
      RGBPixel color = RGBPixelAtPoint(bitmap, x, y); 
      *row++ = color.red; 
      *row++ = color.green; 
      *row++ = color.blue; 
     } 
    } 

    /* Actually write the image data. */ 
    png_init_io(png_ptr, fp); 
    png_set_rows(png_ptr, info_ptr, row_pointers); 
    png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); 

    /* Cleanup. */ 
    for (y = 0; y < bitmap->height; y++) { 
     png_free(png_ptr, row_pointers[y]); 
    } 
    png_free(png_ptr, row_pointers); 

    /* Finish writing. */ 
    png_destroy_write_struct(&png_ptr, &info_ptr); 
    fclose(fp); 
    return 0; 
} 

int main() 
{ 
     RGBBitmap rgbbitmap; 
    int w, h; 
    GetDesktopResolution(w, h); 
    rgbbitmap.height = h; 
    rgbbitmap.width = w; 
    rgbbitmap.bytes_per_pixel = 1; 
    rgbbitmap.bytewidth = w/100; 

    RGBPixel rgbpixel; 
    rgbpixel.blue = 100; 
    rgbpixel.green = 100; 
    rgbpixel.red = 100; 
    rgbbitmap.pixels = &rgbpixel; 

    save_png_to_file(&rgbbitmap, "abc.bmp"); 

     return 0; 
} 

執行此代碼觸發這些錯誤:

LNK1120: 9 unresolved externals

LNK2019: unresolved external symbol _png_create_info_struct referenced in function "int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)" ([email protected]@[email protected]@[email protected])

LNK2019: unresolved external symbol _png_create_write_struct referenced in function "int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)" ([email protected]@[email protected]@[email protected])

LNK2019: unresolved external symbol _png_destroy_write_struct referenced in function "int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)" ([email protected]@[email protected]@[email protected])

LNK2019: unresolved external symbol _png_free referenced in function "int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)" ([email protected]@[email protected]@[email protected])

LNK2019: unresolved external symbol _png_init_io referenced in function "int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)" ([email protected]@[email protected]@[email protected])

LNK2019: unresolved external symbol _png_malloc referenced in function "int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)" ([email protected]@[email protected]@[email protected])

LNK2019: unresolved external symbol _png_set_IHDR referenced in function "int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)" ([email protected]@[email protected]@[email protected])

LNK2019: unresolved external symbol _png_set_rows referenced in function "int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)" ([email protected]@[email protected]@[email protected])

LNK2019: unresolved external symbol _png_write_png referenced in function "int __cdecl save_png_to_file(struct _RGBBitmap *,char const *)" ([email protected]@[email protected]@[email protected])

我米無法找到如何解決這些錯誤。請有任何精彩的建議嗎?

我目前在Windows 7 SP1平臺上使用Visual Studio Ultimate 2013。

非常感謝!

+0

你*已*安裝了[PNG庫](http://www.libpng.org/pub/png/libpng.html)?並將其添加到您的項目? –

+0

@JoachimPileborg,沒錯。 – user3471387

+1

看來你正在使用libpng而不添加庫。在菜單「PROJECT - >屬性 - >連接器 - >輸入 - >附加依賴項」中添加「libpng.lib」並確保庫目錄具有libpng目錄。 – rookiepig

回答

0

我想,你沒有鏈接你的庫,只是包含頭文件。 This question anwers你怎麼辦呢?

如果不是,有plently的可能發生的事情:

  • 你試圖調用這些函數不好參數
  • 你包括壞的頭文件
  • 你已經混庫,或者你正在嘗試鏈接的MinGW,VS2012/VS2012編譯庫VS2013的編譯器,因爲我不知道他們是否兼容...

你可以嘗試download png庫,創建VS2012項目並嘗試編譯它。當你這樣做時,你應該絕對沒有問題,當鏈接...

相關問題