如何将Android.Graphics.Bitmap转换为System.Drawing.Bitmap并返回?
我在Xamarin应用程序(API 28)中获得了以下代码: 我试图找到一种将位图转换为图形对象()的方法,目标是使用插值模式并应用过滤器,然后恢复更改,但我找不到一个关于如何执行的示例 我发现: 但这不是我想要的,因为我得到了以下代码:如何将Android.Graphics.Bitmap转换为System.Drawing.Bitmap并返回?,android,xamarin,graphics,bitmap,Android,Xamarin,Graphics,Bitmap,我在Xamarin应用程序(API 28)中获得了以下代码: 我试图找到一种将位图转换为图形对象()的方法,目标是使用插值模式并应用过滤器,然后恢复更改,但我找不到一个关于如何执行的示例 我发现: 但这不是我想要的,因为我得到了以下代码: private Android.Graphics.Bitmap GetBitmap(Drawable d, System.Drawing.Size? bounds) { ...some code regarding width and height,
private Android.Graphics.Bitmap GetBitmap(Drawable d, System.Drawing.Size? bounds)
{
...some code regarding width and height, and then
var bd = d as BitmapDrawable;
if (bd != null)
{
Android.Graphics.Bitmap src = bd.Bitmap;
if (width == src.Width && height == src.Height)
{
return src;
}
else
{
Graphics myGraphics = Graphics.FromImage(src); // here is the problem
// USE G TO SET InterpolationMode
// convert resulting G somehow back to Android.Graphics.Bitmap, and return it insted of the bottom line
return Android.Graphics.Bitmap.CreateScaledBitmap(src, width, height, true);
}
}
}
错误代码:
Cannot convert from Android.Graphics.Bitmap to System.Drawing.Bitmap
我需要一些关于如何解决这个问题的建议,任何帮助都将不胜感激
谢谢您的时间。您需要先将src转换为Steram。检查以下代码
public static Stream RaiseImage(Android.Graphics.Bitmap bitmap)
{
MemoryStream ms = new MemoryStream();
bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, ms);
return ms;
}
private Android.Graphics.Bitmap GetBitmap(Drawable d, System.Drawing.Size? bounds)
{
// ...some code regarding width and height, and then
var bd = d as BitmapDrawable;
Android.Graphics.Bitmap bitmap = bd.Bitmap;
Stream src= RaiseImage(bitmap);
Image image = System.Drawing.Image.FromStream(src);
Graphics myGraphics = Graphics.FromImage(image); // here is the problem
// USE G TO SET InterpolationMode
// convert resulting G somehow back to Android.Graphics.Bitmap, and return it insted of the bottom line
return Android.Graphics.Bitmap.CreateScaledBitmap(src, width, height, true);
}
感谢您抽出时间,我将此解决方案应用于我的项目,但我遇到了以下错误:此平台不支持此操作。它来自这一行:Image=System.Drawing.Image.FromStream(src);另外,将return语句中的src参数从src改为bitmap。我相信如果我有权访问这些图像,这会起作用,但是我没有,我相信我是从包管理器获得它们的。因此,从技术上讲,这个答案是正确的,但在我的情况下,无论如何,请进行投票:)从System.Drawing.Size中删除
?
?边界。\n它的行为是相同的,有和没有。在我这方面它工作得很好。你使用的是什么平台,xamarin.forms for Android还是xamarin.Android?