Xamarin - 将位图图像保存到资源中

时间:2018-01-24 10:44:19

标签: c# android xamarin

我在行上堆栈bitmap.Compress(Bitmap.CompressFormat.Png,0,stream); 而且我不知道如何继续。以下是我的代码:

Bitmap bitmap = tv.GetDrawingCache(true);
System.IO.MemoryStream stream = new System.IO.MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
Java.IO.File ExternalStorageDirectory = Android.OS.Environment.ExternalStorageDirectory;
Java.IO.File file = new Java.IO.File(ExternalStorageDirectory + Java.IO.File.Separator + "Receipt");
Java.IO.FileOutputStream fileOutputStream = null;
file.CreateNewFile();
fileOutputStream = new Java.IO.FileOutputStream(file);
fileOutputStream.Write(stream.ToArray());

1 个答案:

答案 0 :(得分:0)

  

Xamarin - 将位图图像保存到资源

这是一个简单的演示:

void ExportBitmapAsPNG(Bitmap bitmap)
{
    var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
    var filePath = System.IO.Path.Combine(sdCardPath, "test.png");
    var stream = new FileStream(filePath, FileMode.Create);
    bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
    stream.Close();
}