C# - 调整图像大小的画布(保持源图像的原始像素尺寸) [英] C# - Resize image canvas (keeping original pixel dimensions of source image)

查看:4417
本文介绍了C# - 调整图像大小的画布(保持源图像的原始像素尺寸)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是把图像文件,并增加了尺寸2的下一个动力,同时保留像素,因为它们(又名不结垢源图像)。所以基本上最终的结果将是原始图像,再加上额外的空格跨越了图像,从而使总规模是两个大国的右侧和底部。

My goal is to take an image file and increase the dimensions to the next power of two while preserving the pixels as they are (aka not scaling the source image). So basically the end result would be the original image, plus additional white space spanning off the right and bottom of the image so the total dimensions are powers of two.

以下是我的代码,我现在使用;它创建一个具有正确尺寸的图像,但源数据略微缩放和裁切出于某种原因。

Below is my code that I'm using right now; which creates the image with the correct dimensions, but the source data is slightly scaled and cropped for some reason.

// Load the image and determine new dimensions
System.Drawing.Image img = System.Drawing.Image.FromFile(srcFilePath);
Size szDimensions = new Size(GetNextPwr2(img.Width), GetNextPwr2(img.Height));

// Create blank canvas
Bitmap resizedImg = new Bitmap(szDimensions.Width, szDimensions.Height);
Graphics gfx = Graphics.FromImage(resizedImg);

// Paste source image on blank canvas, then save it as .png
gfx.DrawImageUnscaled(img, 0, 0);
resizedImg.Save(newFilePath, System.Drawing.Imaging.ImageFormat.Png);



这似乎是源图像是基于新的画布大小差异缩小,即使我使用称为DrawImageUnscaled函数()。请告诉我什么我做错了。

It seems like the source image is scaled based on the new canvas size difference, even though I'm using a function called DrawImageUnscaled(). Please inform me of what I'm doing wrong.

推荐答案

该方法 DrawImageUnscaled 不会在原始pizel大小绘制图像,而是使用分辨率源图像和目标图像的(每英寸象素),使得它与在相同的物理尺寸按比例绘制的图像。

The method DrawImageUnscaled doesn't draw the image at the original pizel size, instead it uses the resolution (pixels per inch) of the source and destination images to scale the image so that it's drawn with the same physical dimensions.

使用的DrawImage 方法,而不是使用原来的像素大小绘制图像:

Use the DrawImage method instead to draw the image using the original pixel size:

gfx.DrawImage(img, 0, 0, img.Width, img.Height);

这篇关于C# - 调整图像大小的画布(保持源图像的原始像素尺寸)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆