使用libpng和GDI读取显示png图片

网上都是gdiplus的例子,其实GDI也是支持透明度的,放一个例子。#include "png.h"#include "zlib.h"#include #pragma comment(lib, "Msimg32.lib")//AlphaBlend需要void Draw(HWND hwnd, const char* pngfile, bool layered){
摘要由CSDN通过智能技术生成

网上都是gdiplus的例子,其实GDI也是支持透明度的,放一个例子。


#include "png.h"
#include "zlib.h"
#include <fstream>

#pragma comment(lib, "Msimg32.lib")//AlphaBlend需要

void Draw(HWND hwnd, const char* pngfile, bool layered)
{
    png_image image;
    memset(&image, 0, (sizeof image));
    image.version = PNG_IMAGE_VERSION;
    if (!png_image_begin_read_from_file(&image, pngfile)) {
        printf("read file error!");
        return;
    }
    png_bytep buffer = (png_bytep)malloc(PNG_IMAGE_SIZE(image));
    //读完一行前进的字节数, 因为bmp里面是反向扫描的, 这里直接设置为负数反向读取
    png_int_32 row_stride = -(png_int_32)PNG_IMAGE_ROW_STRIDE(image);
    if (buffer && !png_image_finish_read(&image, NULL, buffer, row_stride, NULL)) {
        printf("read png file error!");
        free(buffer);
        return;
    }
    //图片有透明度, 颜色需要预乘, 同时把RGBA转为BGRA
    png_bytep buffer2 = buffer;
    
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 首先,需要安装libpng库,可以通过以下命令在Ubuntu上安装: ``` sudo apt-get install libpng-dev ``` 2. 接下来,需要编写一个C程序来读取PNG图像。以下是一个简单的程序示例: ```c #include <stdio.h> #include <stdlib.h> #include <png.h> int main(int argc, char *argv[]) { if (argc != 2) { printf("Usage: %s <filename>\n", argv[0]); return 1; } char *filename = argv[1]; FILE *fp = fopen(filename, "rb"); if (!fp) { printf("Failed to open %s\n", filename); return 1; } png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) { printf("Failed to create PNG read struct\n"); fclose(fp); return 1; } png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { printf("Failed to create PNG info struct\n"); png_destroy_read_struct(&png_ptr, NULL, NULL); fclose(fp); return 1; } png_init_io(png_ptr, fp); png_read_info(png_ptr, info_ptr); int width = png_get_image_width(png_ptr, info_ptr); int height = png_get_image_height(png_ptr, info_ptr); png_byte color_type = png_get_color_type(png_ptr, info_ptr); png_byte bit_depth = png_get_bit_depth(png_ptr, info_ptr); printf("Image size: %d x %d\n", width, height); printf("Color type: %d\n", color_type); printf("Bit depth: %d\n", bit_depth); png_bytep row_pointers[height]; for (int y = 0; y < height; y++) { row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_ptr, info_ptr)); } png_read_image(png_ptr, row_pointers); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { png_bytep pixel = &(row_pointers[y][x * 4]); printf("(%d,%d): %d %d %d %d\n", x, y, pixel[0], pixel[1], pixel[2], pixel[3]); } } for (int y = 0; y < height; y++) { free(row_pointers[y]); } png_destroy_read_struct(&png_ptr, &info_ptr, NULL); fclose(fp); return 0; } ``` 3. 编译程序: ``` gcc -o read_png read_png.c -lpng ``` 4. 运行程序: ``` ./read_png example.png ``` 注意,以上示例程序仅供参考,实际应用中需要根据具体需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值