在c#中调整图像大小不起作用
本文关键字:不起作用 图像 调整 | 更新日期: 2023-09-27 18:01:31
我正在尝试将图像调整为width=100
height=100
public ActionResult RegisterUser(userAuthModel user, HttpPostedFileBase userimage)
{
if (userimage.ContentLength > 0 && userimage != null)
{
string fname = userimage.FileName;
var path = Server.MapPath("~/Images/User_DP/" + userimage.FileName);
var image_100 = new System.Drawing.Bitmap(userimage, new Size(100, 100));
userimage.SaveAs(path);
}
.
.
.
.
.
}
这里我使用位图方法来调整图像的宽度和高度。
但是这显示了错误行-
var image_100 = new System.Drawing.Bitmap(userimage, new Size(100, 100));
无效参数的。我如何使用位图调整图像大小?
System.Drawing.Bitmap
类要求在构造函数中使用两个参数:
System.Drawing.Image
System.Drawing.Size
在您的代码中,您正确地传递了大小,但不是图像。HttpPostedFileBase
不扩展Image类。
您将需要更改代码的这一部分。如果您使用HttpPostedFileBase
作为流,那么请记住System.Drawing.Bitmap
没有要求Stream
和Size
的构造函数。
引用:
System.Drawing.Image
System.Web.HttpPostedFileBase
你必须使用上述方法,它可能会帮助你
private static BitmapFrame CreateResizedImage(ImageSource source, int width, int height, int margin)
{
var rect = new Rect(margin, margin, width - margin * 2, height - margin * 2);
var group = new DrawingGroup();
RenderOptions.SetBitmapScalingMode(group, BitmapScalingMode.HighQuality);
group.Children.Add(new ImageDrawing(source, rect));
var drawingVisual = new DrawingVisual();
using (var drawingContext = drawingVisual.RenderOpen())
drawingContext.DrawDrawing(group);
var resizedImage = new RenderTargetBitmap(
width, height, // Resized dimensions
96, 96, // Default DPI values
PixelFormats.Default); // Default pixel format
resizedImage.Render(drawingVisual);
return BitmapFrame.Create(resizedImage);
}