简体   繁体   English

将布局保存到位图(Xamarin)

[英]Save Layout to bitmap(Xamarin)

I have layout and need to save it to bitmap 我有布局,需要将其保存到位图

here is code 这是代码

public void Save()
{
    LinearLayout view = FindViewById<LinearLayout>(Resource.Id.badge);

    view.DrawingCacheEnabled = true;
    view.BuildDrawingCache();
    Bitmap layout = view.GetDrawingCache(true);

}

I set breakpoint to Bitmap layout = view.GetDrawingCache(true); 我将断点设置为Bitmap layout = view.GetDrawingCache(true); and I see that's layout is null. 我看到它的布局是空的。

Where is my mistake, how to save layout to bitmap? 我的错误在哪里,如何将布局保存到位图?

UPDATE UPDATE

I try to save view to bitmap like this 我尝试将视图保存到这样的位图

public  Bitmap CreateBitmapFromView(View view, bool autoScale = true)
    {
        var wasDrawingCacheEnabled = view.DrawingCacheEnabled;
        view.DrawingCacheEnabled = true;
        view.BuildDrawingCache(autoScale);
        var bitmap2 = view.GetDrawingCache(autoScale);
        view.DrawingCacheEnabled = wasDrawingCacheEnabled;
        return bitmap2;

    }

All ok, bitmap2 is returning. 好的,bitmap2正在返回。

But also I need to save it to SD 但我还需要将它保存到SD

I wrote method like this 我写了这样的方法

public  Bitmap CreateBitmapFromView(View view, bool autoScale = true)
    {
        var wasDrawingCacheEnabled = view.DrawingCacheEnabled;
        view.DrawingCacheEnabled = true;
        view.BuildDrawingCache(autoScale);
        var bitmap2 = view.GetDrawingCache(autoScale);
        view.DrawingCacheEnabled = wasDrawingCacheEnabled;
        var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
        var filePath = System.IO.Path.Combine(sdCardPath, "test.png");
        var stream = new FileStream(filePath, FileMode.Create);
        bitmap2.Compress(Bitmap.CompressFormat.Png, 100, stream);
        return bitmap2;

    }

And have this error. 并有这个错误。

Object reference not set to an instance of an object. 你调用的对象是空的。

change it to something likes this : 把它改成这样的东西:

public Bitmap CreateBitmapFromView(View view, bool autoScale = true)
{
    var wasDrawingCacheEnabled = view.DrawingCacheEnabled;
    view.DrawingCacheEnabled = true;
    view.BuildDrawingCache(autoScale);
    var bitmap = view.GetDrawingCache(autoScale);
    view.DrawingCacheEnabled = wasDrawingCacheEnabled;
    return bitmap;
}

UPDATE UPDATE

try to define var bitmap2 as global variable 尝试将var bitmap2定义为全局变量

public  Bitmap CreateBitmapFromView(View view, bool autoScale = true)
    {
        var wasDrawingCacheEnabled = view.DrawingCacheEnabled;
        view.DrawingCacheEnabled = true;
        view.BuildDrawingCache(autoScale);
        view.DrawingCacheEnabled = wasDrawingCacheEnabled;
        var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
        var filePath = System.IO.Path.Combine(sdCardPath, "test.png");
        var stream = new FileStream(filePath, FileMode.Create);
        bitmap2.Compress(Bitmap.CompressFormat.Png, 100, stream);
        return bitmap2;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM