Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-调整图像画布的大小(保持源图像的原始像素尺寸)_C#_Image_Graphics_Image Processing_Drawing - Fatal编程技术网

C#-调整图像画布的大小(保持源图像的原始像素尺寸)

C#-调整图像画布的大小(保持源图像的原始像素尺寸),c#,image,graphics,image-processing,drawing,C#,Image,Graphics,Image Processing,Drawing,我的目标是获取一个图像文件,并将尺寸增加到下一个二次方,同时保持像素不变(即不缩放源图像)。基本上,最终的结果是原始图像,加上图像右侧和底部的额外空白,所以总尺寸是二的幂 下面是我现在正在使用的代码;这将创建具有正确尺寸的图像,但由于某些原因,源数据会略微缩放和裁剪 // Load the image and determine new dimensions System.Drawing.Image img = System.Drawing.Image.FromFile(srcFilePath)

我的目标是获取一个图像文件,并将尺寸增加到下一个二次方,同时保持像素不变(即不缩放源图像)。基本上,最终的结果是原始图像,加上图像右侧和底部的额外空白,所以总尺寸是二的幂

下面是我现在正在使用的代码;这将创建具有正确尺寸的图像,但由于某些原因,源数据会略微缩放和裁剪

// 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()的函数。请告诉我我做错了什么。

方法
DrawImageUnscaled
不会以原始pizel大小绘制图像,而是使用源图像和目标图像的分辨率(每英寸像素)缩放图像,以便以相同的物理尺寸绘制图像

改用
DrawImage
方法使用原始像素大小绘制图像:

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

使用
DrawImage
,使用其中一个重载,在其中明确指定目标矩形(使用与原始源图像相同大小的矩形)

请参阅:

我有一段时间没有在C#中工作了,但我记得还有x和y的图像分辨率DPI参数可以设置-它们可能会扭曲图像。。