如何调整图像大小 C#
由于 Size
、Width
和 Height
是 System.Drawing.Image
Get() 属性代码>;
如何在 C# 中在运行时调整 Image 对象的大小?
现在,我只是使用以下命令创建一个新的 Image
:
// objImage is the original Image
Bitmap objBitmap = new Bitmap(objImage, new Size(227, 171));
As Size
, Width
and Height
are Get()
properties of System.Drawing.Image
;
How can I resize an Image object at run-time in C#?
Right now, I am just creating a new Image
using:
// objImage is the original Image
Bitmap objBitmap = new Bitmap(objImage, new Size(227, 171));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
这将执行高质量的调整大小:
wrapMode.SetWrapMode(WrapMode.TileFlipXY)
防止图像边界周围出现重影——简单的调整大小会对超出图像边界的透明像素进行采样,但通过镜像图像我们可以获得更好的样本(此设置是非常明显)destImage.SetResolution
无论物理尺寸如何,都保持 DPI - 在减小图像尺寸或打印时可能会提高质量。
graphics.CompositingMode
确定源图像中的像素是否覆盖或与背景像素组合。
SourceCopy
指定渲染颜色时,它会覆盖背景颜色。graphics.CompositingQuality
决定分层图像的渲染质量级别。
graphics.InterpolationMode
< /a> 确定如何计算两个端点之间的中间值graphics.SmoothingMode
指定直线、曲线和填充区域的边缘是否使用平滑(也称为抗锯齿)——可能仅适用于矢量graphics.PixelOffsetMode
影响绘制时的渲染质量新图像保持纵横比留给读者作为练习(实际上,我只是不认为这个函数的工作是为你做这件事)。
另外,这是一篇好文章,描述了图像调整大小的一些陷阱。上面的函数已经涵盖了大部分,但是你仍然需要担心 保存。
This will perform a high quality resize:
wrapMode.SetWrapMode(WrapMode.TileFlipXY)
prevents ghosting around the image borders -- naïve resizing will sample transparent pixels beyond the image boundaries, but by mirroring the image we can get a better sample (this setting is very noticeable)destImage.SetResolution
maintains DPI regardless of physical size -- may increase quality when reducing image dimensions or when printinggraphics.CompositingMode
determines whether pixels from a source image overwrite or are combined with background pixels.SourceCopy
specifies that when a color is rendered, it overwrites the background color.graphics.CompositingQuality
determines the rendering quality level of layered images.graphics.InterpolationMode
determines how intermediate values between two endpoints are calculatedgraphics.SmoothingMode
specifies whether lines, curves, and the edges of filled areas use smoothing (also called antialiasing) -- probably only works on vectorsgraphics.PixelOffsetMode
affects rendering quality when drawing the new imageMaintaining aspect ratio is left as an exercise for the reader (actually, I just don't think it's this function's job to do that for you).
Also, this is a good article describing some of the pitfalls with image resizing. The above function will cover most of them, but you still have to worry about saving.
不知道这有什么困难,做你正在做的事情,使用重载的位图构造函数创建一个重新调整大小的图像,你唯一缺少的是转换回图像数据类型:
Not sure what is so difficult about this, do what you were doing, use the overloaded Bitmap constructor to create a re-sized image, the only thing you were missing was a cast back to the Image data type:
在这个问题中,你会得到一些答案,包括我的:
in this question, you'll have some answers, including mine:
您可以尝试我的库 net-vips,libvips。它是一个惰性的、流式的、需求驱动的图像处理库,因此它可以执行这样的操作,而无需加载整个图像。
例如,它配备了一个方便的图像缩略图:
它还支持智能裁剪,这是一种智能确定图像最重要部分并在裁剪图像时保持其焦点的方法。例如:
其中
owl.jpg
是偏心构图:给出以下结果:
首先,它缩小图像以使垂直轴达到 128 像素,然后使用注意力策略将宽度裁剪为 128 像素。此搜索会在图像中搜索可能吸引人眼的特征,请参阅
Smartcrop()
了解详细信息。You could try my library net-vips, the C# binding for libvips. It's a lazy, streaming, demand-driven image processing library, so it can do operations like this without needing to load the whole image.
For example, it comes with a handy image thumbnailer:
It also supports smart crop, a way of intelligently determining the most important part of the image and keeping it in focus while cropping the image. For example:
Where
owl.jpg
is an off-centre composition:Gives this result:
First it shrinks the image to get the vertical axis to 128 pixels, then crops down to 128 pixels across using the
attention
strategy. This one searches the image for features which might catch a human eye, seeSmartcrop()
for details.为什么不使用 System.Drawing.Image.GetThumbnailImage 方法?
示例:
来源:
http://msdn.microsoft.com/en-us /library/system.drawing.image.getthumbnailimage.aspx
Why not use the
System.Drawing.Image.GetThumbnailImage
method?Example:
Source:
http://msdn.microsoft.com/en-us/library/system.drawing.image.getthumbnailimage.aspx
这将 -
///////////////
This will -
//////////////
此代码与上述答案之一发布的代码相同..但会将透明像素转换为白色而不是黑色...谢谢:)
This code is same as posted from one of above answers.. but will convert transparent pixel to white instead of black ... Thanks:)
这是我针对特定要求编写的代码,即:目的地始终处于横向比例。它应该给你一个良好的开始。
This is the code that I worked out for a specific requirement ie: the destination is always in landscape ratio. It should give you a good start.
在我制作的应用程序中,有必要创建一个具有多个选项的函数。它很大,但它可以调整图像大小,可以保持纵横比,并且可以剪切边缘以仅返回图像的中心:
为了更容易访问该函数,可以添加一些重载函数:
现在是最后一个两个布尔值可选设置。
像这样调用该函数:
In the application I made it was necessary to create a function with multiple options. It's quite large, but it resizes the image, can keep the aspect ratio and can cut of the edges to return only the center of the image:
To make it easier to acces the function it's possible to add some overloaded functions:
Now are the last two booleans optional to set.
Call the function like this:
单击此处 http://bhupendrasinghsaini.blogspot.in/2014/07 /resize-image-in-c.html
Click here http://bhupendrasinghsaini.blogspot.in/2014/07/resize-image-in-c.html
如果您使用的是 BitmapSource:
如果您想更好地控制质量,请先运行此命令:(
默认为 BitmapScalingMode.Linear,相当于 BitmapScalingMode。低质量。)
If you're working with a
BitmapSource
:If you want finer control over quality, run this first:
(Default is
BitmapScalingMode.Linear
which is equivalent toBitmapScalingMode.LowQuality
.)注意:这不适用于 ASP.Net Core,因为 WebImage 依赖于 System.Web,但在以前版本的 ASP.Net 上,我多次使用此代码片段并且很有用。
Note: this will not work with ASP.Net Core because WebImage depends on System.Web, but on previous versions of ASP.Net I used this snippet many times and was useful.
下面的函数将返回显示图像的新尺寸。这在这里可能没有帮助。但它将返回调整大小的显示矩形尺寸。
Below function will return the new size to display the image.This may not be helpful here.But it will return resized Display Rectangle size.
调整图像大小并保存以适应宽度和高度,就像画布一样保持图像比例
Resize and save an image to fit under width and height like a canvas keeping image proportional
使用以下函数和以下示例来更改图像大小:
Use below function with below example for changing image size :
为了完整起见,添加另一个答案
对于现代 .NET 核心应用程序,使用 System.Drawing 并不是最佳选择,特别是如果您需要在不依赖操作系统的情况下跨平台调整大小(在本例中为 libgdi)。
我过去使用过
ImageSharp
库(它是 100% 托管代码所以没有操作系统依赖性)。然而,对于我们的用例来说,它使用了太多内存。这就是我们改用
PhotoSauce
的原因。该库确实依赖于本机编解码器,但它与 nuget-package 一起很好地分发,并且可以跨平台工作(在 win64、linux64 各种发行版上进行了测试)。PS 我不隶属于上述任何一个,只是想推荐一个很棒的产品(PhotoSauce)。
Adding another answer for the sake of completeness
For modern .NET core apps, using
System.Drawing
is not the best choice especially if you need cross-platform resizing without OS dependencies (libgdi in this case).I've used
ImageSharp
library in the past (it's 100% managed code so no OS dependencies). However it uses too much memory for our use case.That is why we switched to
PhotoSauce
. This library does depend on a native codec, but it is distributed nicely with the nuget-package and works cross-platform (tested on win64, linux64 various distros).P.S. I'm not affiliated with any of the above just wanted to recommend an awesome product (PhotoSauce).