c# - 如何调整图像大小以填充新尺寸?
问题描述
我正在尝试拉伸图像以适应新尺寸,但我无法弄清楚如何使图像填充新尺寸,它只会创建更大的图像尺寸但它保持图像不变,我想要图像填充指定的宽度和高度。
private Bitmap resizeImage(Image image, int width, int height, float HorizontalResolution, float VerticalResolution)
{
Rectangle destRect = new Rectangle(0, 0, width, height);
Bitmap destImage = new Bitmap(width, height);
destImage.SetResolution(HorizontalResolution, VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
解决方案
由于您想将整个源图像拉伸到指定大小,只需使用不接受源坐标的简单五参数重载:
像这样:
graphics.DrawImage(image, 0, 0, width, height);
推荐阅读
- c++ - 使用犰狳忽略平均值和其他统计函数中的 NaN
- awesome-wm - Awesomewm:如何在默认情况下使不可聚焦的客户端(Ubuntu 中的 google-chrome)成为可聚焦的?
- paypal - 有人可以伪造贝宝付款吗?
- video - 为什么 Superagent 不保留有关这部电影的任何信息?
- html - 将导航栏中的内容对齐到顶部
- jquery - 基于视口宽度的行高的 CSS calc
- php - 使用 PHP 和 mySQLi 的手工计数器脚本的问题
- python - 如何在 settings.py 文件中隐藏 EMAIL_HOST_PASSWORD?
- android - 使用 Dagger 将 Activity 绑定到 LifecycleOwner
- ionic-framework - Ionic 中的嵌套 JSON 数据