如何解决图像自动裁剪,出现边框问题
我正在应用程序中谈论照片。拍摄完后,我希望在布局的下一个屏幕上像图像一样自动裁剪。 但是我一直在迷失照片的边界,而且边界也很奇怪(图像中用红色表示)。
这是我的代码:
Android code
private void NewElement_OnDrawBitmap(object sender,EventArgs e)
{
if (this.ViewGroup != null)
{
//get the subview
Android.Views.View subView = ViewGroup.GetChildAt(0);
int width = subView.Width;
int height = subView.Height;
//create and draw the bitmap
Bitmap b = Bitmap.CreateBitmap(width,height,Bitmap.Config.Argb8888);
Canvas c = new Canvas(b);
ViewGroup.Draw(c);
//save the bitmap to file
bytes = SaveBitmapToFile(b);
}
}
iOS code
UIGraphics.BeginImageContextWithOptions(this.Bounds.Size,true,0);
this.Layer.RenderInContext(UIGraphics.GetCurrentContext());
var img = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
using (NSData imageData = img.AsPNG())
{
bytes = new Byte[imageData.Length];
System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes,bytes,Convert.ToInt32(imageData.Length));
}
private byte[] SaveBitmapToFile(Bitmap bm)
{
MemoryStream ms = new MemoryStream();
bm.Compress(Bitmap.CompressFormat.Png,100,ms);
return ms.ToArray();
}
}
解决方法
从共享代码中,您没有进行任何裁剪,因此您的应用程序仅将原始图片放入视图中,并且您提到的边框可能是视图的背景。 还有您的项目代码,我看到您添加了
Bitmap bitmap = Bitmap.CreateBitmap(b,(98 * width) / 100,(82 * height) / 100);
是的,这是裁剪图片,但这只是将图像从左上角裁剪到98%的宽度和82%的高度。很像
如果您想将图片裁剪为一个中心对准的正方形,则可以简单地进行以下操作:
int offset = (height - width) / 2;
Bitmap bitmap = Bitmap.CreateBitmap(b,offset,width,height- offset);
但是,如果您想对图像执行更多操作,例如移动/缩放...,然后进行裁剪,我建议您使用FFImageLoading或Syncfusion等现有解决方案。否则,您必须计算所有移动/缩放数据才能进行裁剪。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。