1

I have Xamarin Android app.

I try to save Linear layout like 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 need to save it to Pictures folder. How I can do this?

2 Answers2

0

He explained the way to save bitmap as png in memory card with c #. I hope to understand your problem is correct.

"This here is a slim way to export a Bitmap as PNG-file to the sd-card using only C# stuff"

https://stackoverflow.com/a/29012075/6322661

Community
  • 1
  • 1
Mahmut Bedir
  • 487
  • 1
  • 5
  • 23
0

You can use Canvas to draw a View by the following code:

    public Bitmap createViewBitmap(View v)
    {
        Bitmap bitmap = Bitmap.CreateBitmap(v.Width, v.Height,
                Bitmap.Config.Argb8888);
        Canvas canvas = new Canvas(bitmap);
        v.Draw(canvas);
        return bitmap;
    }

Linear layout is a kind of View. So you can create a Linear layout BitMap :

 View v = FindViewById<LinearLayout>(Resource.Id.myLinearLayout);
 Bitmap myBitMap = createViewBitmap(v);

And then save it in the DCIM folder:

 public static void saveImage(Bitmap bmp)
 {
      try
      {
          using (var os = new System.IO.FileStream(Android.OS.Environment.ExternalStorageDirectory + "/DCIM/Camera/MikeBitMap2.jpg", System.IO.FileMode.CreateNew))
           {
               bmp.Compress(Bitmap.CompressFormat.Jpeg, 95, os);
           }
       }
       catch (Exception e)
       {

       }
   }

enter image description here

You can refer to my github for the more code information.

Mike Ma
  • 2,017
  • 1
  • 12
  • 17
  • Your code is great, I have one simple problem, I have this error `Java.Lang.IllegalArgumentException: width and height must be > 0` –  Mar 20 '17 at 07:55
  • You may call `createViewBitmap(v)` in the `OnCreate`. This view does not draw completed. In my demo I call the function in the click event. – Mike Ma Mar 20 '17 at 08:24
  • refer to answer http://stackoverflow.com/questions/3591784/getwidth-and-getheight-of-view-returns-0 may help you to understand. – Mike Ma Mar 20 '17 at 08:27