java memorystream 包_存储在MemoryStream中的裁剪图像中心

我有一个图像上传表格,存储两种尺寸的图像:大图像和缩略图图像 .

对于缩略图图像,我试图从中心裁剪图像并重新调整大小为:30px x 30px .

这是我的代码:

private static Bitmap ResizeImage(MemoryStream uploadStream, int maxWidth, int maxHeight)

{

Image img = Image.FromStream(uploadStream);

double ratioX = (double)maxWidth / img.Width;

double ratioY = (double)maxHeight / img.Height;

double ratio = Math.Max(ratioX, ratioY);

int newWidth = (int)(img.Width * ratio);

int newHeight = (int)(img.Height * ratio);

Bitmap resizedBitmap = new Bitmap(newWidth, newHeight);

Graphics.FromImage(resizedBitmap).DrawImage(img, 0, 0, newWidth, newHeight);

img.Dispose();

return resizedBitmap;

}

private static Bitmap CropImageToCentre(MemoryStream uploadStream, int width, int height)

{

Image img = Image.FromStream(uploadStream);

Bitmap resizedBitmap = new Bitmap(img);

int StartX = 0, StartY = 0;

int EndX = img.Width, EndY = img.Height;

bool Crop = false;

if (img.Width > width)

{

int MidX = img.Width / 2;

StartX = MidX - (width / 2);

EndX = MidX + (width / 2);

Crop = true;

}

if (img.Width > height)

{

int MidY = img.Height / 2;

StartY = MidY - (height / 2);

EndY = MidY + (height / 2);

Crop = true;

}

if (Crop)

{

Size imgSize = new Size(width, height);

resizedBitmap = new Bitmap(img, imgSize);

}

img.Dispose();

return resizedBitmap;

}

public static Bitmap ResizeThumbnail(MemoryStream ms)

{

int thumbWidth = int.Parse(ConfigurationManager.AppSettings["thumbwidth"]);

int thumbHeight = int.Parse(ConfigurationManager.AppSettings["thumbheight"]);

return CropImageToCentre(BitmapToMemoryStream(ResizeImage(ms, thumbWidth, thumbHeight)), thumbWidth, thumbHeight);

}

public static Bitmap ResizeLargeImage(MemoryStream ms)

{

int width = int.Parse(ConfigurationManager.AppSettings["largewidth"]);

int height = int.Parse(ConfigurationManager.AppSettings["largeheight"]);

return ResizeImage(ms, width, height);

}

private static MemoryStream BitmapToMemoryStream(Bitmap bm)

{

MemoryStream memoryStream = new MemoryStream();

bm.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);

return memoryStream;

}

我遇到的问题是在调用 ResizeThumbnail() 方法时,图像不会被裁剪或调整为30px的高度和宽度 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值