我在Xamarin应用程序(API 28(中得到了这段代码:
我试图找到一种将位图转换为图形对象的方法(https://learn.microsoft.com/en-us/dotnet/api/system.drawing.graphics?view=dotnet-plat-ext-3.1(,目标是使用插值模式并应用过滤器,然后恢复更改,但我没有找到一个关于如何做到这一点的例子。
我发现:将图形对象转换为位图对象但这不是我想要的,因为,我得到了这个代码:
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);
}