目前我正在创建一个可以拍摄base64图像并将其裁剪为所需矩形(X,Y,宽度,高度)的函数 . 但是,下面的代码似乎没有做到这一点,我不知道为什么 . 它返回图像不变和未剪切 .
谁能看到这个问题? :)
public static string CropImage(string base64, int x, int y, int width, int height)
{
byte[] bytes = Convert.FromBase64String(base64);
using (var ms = new MemoryStream(bytes))
{
Bitmap bmp = new Bitmap(ms);
Rectangle rect = new Rectangle(x, y, width, height);
Bitmap croppedBitmap = new Bitmap(rect.Width, rect.Height, bmp.PixelFormat);
using (Graphics gfx = Graphics.FromImage(croppedBitmap))
{
gfx.DrawImage(bmp, 0, 0, rect, GraphicsUnit.Pixel);
}
using (MemoryStream ms2 = new MemoryStream())
{
bmp.Save(ms2, ImageFormat.Jpeg);
byte[] byteImage = ms2.ToArray();
var croppedBase64 = Convert.ToBase64String(byteImage);
return croppedBase64;
}
}
}
1 回答
裁剪后的图像位于
croppedBitmap
,bmp
是原始图像 . 我想你想在第二个内存流中使用croppedBitmap
: