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);
and I see that's layout is null.
Where is my mistake, how to save layout to bitmap?
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.
But also I need to save it to 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
try to define var bitmap2
as global variable
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.