C# 如何在xamarin Android中裁剪旋转图像?
我的要求是在旋转后裁剪图像 样本: 我的真实形象: 轮换后: 在这里,我将图像旋转了45度。现在我只想裁剪高亮显示的(红色矩形)矩形区域。 代码:C# 如何在xamarin Android中裁剪旋转图像?,c#,android,xamarin.android,image-rotation,C#,Android,Xamarin.android,Image Rotation,我的要求是在旋转后裁剪图像 样本: 我的真实形象: 轮换后: 在这里,我将图像旋转了45度。现在我只想裁剪高亮显示的(红色矩形)矩形区域。 代码: 请告诉我如何在旋转后找到图像坐标点。您可以裁剪屏幕视图并自定义屏幕框架 例如,CustomShot方法如下: public Bitmap CustomShot(Activity activity, int x, int y, int w, int h) { // get the top view of windows View v
请告诉我如何在旋转后找到图像坐标点。您可以裁剪屏幕视图并自定义屏幕框架 例如,
CustomShot
方法如下:
public Bitmap CustomShot(Activity activity, int x, int y, int w, int h)
{
// get the top view of windows
View view = activity.Window.DecorView;
view.BuildDrawingCache();
// get the height of status bar
Rect rect = new Rect();
view.GetWindowVisibleDisplayFrame(rect);
int statusBarHeights = rect.Top;
Display display = activity.WindowManager.DefaultDisplay;
// get the width and height of screen
if (w == 0)
{
w = display.Width;
}
int heights = display.Height;
// allow cache
view.DrawingCacheEnabled = true;
// custom the crop frame
Bitmap bmp = Bitmap.CreateBitmap(view.GetDrawingCache(true), x,
y, w, h);
// clear cache
view.DestroyDrawingCache();
return bmp;
}
private void Cropbutton_Click(object sender, System.EventArgs e)
{
if(imageBitmap == null){
imageBitmap = ViewToBitmap();
this.imageView.SetImageBitmap(GetCroppedBitmap(imageBitmap, points));
}else{
this.imageView.SetImageBitmap(CustomShot( this, 0, 300, 0, 500));
this.imageView.SetScaleType(ImageView.ScaleType.FitCenter);
this.imageView.Rotation = 0;
}
}
然后在Cropbutton\u单击方法中使用,如下所示:
public Bitmap CustomShot(Activity activity, int x, int y, int w, int h)
{
// get the top view of windows
View view = activity.Window.DecorView;
view.BuildDrawingCache();
// get the height of status bar
Rect rect = new Rect();
view.GetWindowVisibleDisplayFrame(rect);
int statusBarHeights = rect.Top;
Display display = activity.WindowManager.DefaultDisplay;
// get the width and height of screen
if (w == 0)
{
w = display.Width;
}
int heights = display.Height;
// allow cache
view.DrawingCacheEnabled = true;
// custom the crop frame
Bitmap bmp = Bitmap.CreateBitmap(view.GetDrawingCache(true), x,
y, w, h);
// clear cache
view.DestroyDrawingCache();
return bmp;
}
private void Cropbutton_Click(object sender, System.EventArgs e)
{
if(imageBitmap == null){
imageBitmap = ViewToBitmap();
this.imageView.SetImageBitmap(GetCroppedBitmap(imageBitmap, points));
}else{
this.imageView.SetImageBitmap(CustomShot( this, 0, 300, 0, 500));
this.imageView.SetScaleType(ImageView.ScaleType.FitCenter);
this.imageView.Rotation = 0;
}
}
其效果是: