Size, Width和Height都是System.Drawing.Image的Get()属性; 我如何在c#运行时调整图像对象的大小?
现在,我只是创建一个新的图像使用:
// objImage is the original Image
Bitmap objBitmap = new Bitmap(objImage, new Size(227, 171));
Size, Width和Height都是System.Drawing.Image的Get()属性; 我如何在c#运行时调整图像对象的大小?
现在,我只是创建一个新的图像使用:
// objImage is the original Image
Bitmap objBitmap = new Bitmap(objImage, new Size(227, 171));
当前回答
你可以试试net-vips, libvips的c#绑定。它是一个懒惰的、流的、需求驱动的图像处理库,所以它可以做这样的操作,而不需要加载整个图像。
例如,它附带了一个方便的图像缩略图:
using Image image = Image.Thumbnail("image.jpg", 300, 300);
image.WriteToFile("my-thumbnail.jpg");
它还支持智能裁剪,这是一种智能确定图像中最重要的部分并在裁剪图像时保持对焦的方法。例如:
using Image image = Image.Thumbnail("owl.jpg", 128, crop: Enums.Interesting.Attention);
image.WriteToFile("tn_owl.jpg");
其中owl.jpg是一个偏离中心的合成:
给出如下结果:
首先,它将图像的垂直轴缩小到128像素,然后使用注意力策略将图像缩小到128像素。这一个搜索图像中可能吸引人眼的特征,详见Smartcrop()。
其他回答
这是我为特定要求编写的代码,即:目的地总是在景观比例。这会给你一个好的开始。
public Image ResizeImage(Image source, RectangleF destinationBounds)
{
RectangleF sourceBounds = new RectangleF(0.0f,0.0f,(float)source.Width, (float)source.Height);
RectangleF scaleBounds = new RectangleF();
Image destinationImage = new Bitmap((int)destinationBounds.Width, (int)destinationBounds.Height);
Graphics graph = Graphics.FromImage(destinationImage);
graph.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
// Fill with background color
graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), destinationBounds);
float resizeRatio, sourceRatio;
float scaleWidth, scaleHeight;
sourceRatio = (float)source.Width / (float)source.Height;
if (sourceRatio >= 1.0f)
{
//landscape
resizeRatio = destinationBounds.Width / sourceBounds.Width;
scaleWidth = destinationBounds.Width;
scaleHeight = sourceBounds.Height * resizeRatio;
float trimValue = destinationBounds.Height - scaleHeight;
graph.DrawImage(source, 0, (trimValue / 2), destinationBounds.Width, scaleHeight);
}
else
{
//portrait
resizeRatio = destinationBounds.Height/sourceBounds.Height;
scaleWidth = sourceBounds.Width * resizeRatio;
scaleHeight = destinationBounds.Height;
float trimValue = destinationBounds.Width - scaleWidth;
graph.DrawImage(source, (trimValue / 2), 0, scaleWidth, destinationBounds.Height);
}
return destinationImage;
}
使用下面的函数和下面的例子来改变图像大小:
//Example :
System.Net.Mime.MediaTypeNames.Image newImage = System.Net.Mime.MediaTypeNames.Image.FromFile("SampImag.jpg");
System.Net.Mime.MediaTypeNames.Image temImag = FormatImage(newImage, 100, 100);
//image size modification unction
public static System.Net.Mime.MediaTypeNames.Image FormatImage(System.Net.Mime.MediaTypeNames.Image img, int outputWidth, int outputHeight)
{
Bitmap outputImage = null;
Graphics graphics = null;
try
{
outputImage = new Bitmap(outputWidth, outputHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
graphics = Graphics.FromImage(outputImage);
graphics.DrawImage(img, new Rectangle(0, 0, outputWidth, outputHeight),
new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
return outputImage;
}
catch (Exception ex)
{
return img;
}
}
如果你正在使用BitmapSource:
var resizedBitmap = new TransformedBitmap(
bitmapSource,
new ScaleTransform(scaleX, scaleY));
如果你想要更好的控制质量,首先运行这个:
RenderOptions.SetBitmapScalingMode(
bitmapSource,
BitmapScalingMode.HighQuality);
(默认为BitmapScalingMode。线性,相当于BitmapScalingMode.LowQuality。)
注意:这将不适用于ASP。因为WebImage依赖于系统。Web,但在以前版本的ASP。我在Net中多次使用了这个片段,它很有用。
String ThumbfullPath = Path.GetFileNameWithoutExtension(file.FileName) + "80x80.jpg";
var ThumbfullPath2 = Path.Combine(ThumbfullPath, fileThumb);
using (MemoryStream stream = new MemoryStream(System.IO.File.ReadAllBytes(fullPath)))
{
var thumbnail = new WebImage(stream).Resize(80, 80);
thumbnail.Save(ThumbfullPath2, "jpg");
}
不知道这有什么困难,做你正在做的,使用重载Bitmap构造函数来创建一个重新大小的图像,你唯一缺少的是转换回image数据类型:
public static Image resizeImage(Image imgToResize, Size size)
{
return (Image)(new Bitmap(imgToResize, size));
}
yourImage = resizeImage(yourImage, new Size(50,50));