2012-09-24 162 views
0

所以我試圖創建一個功能,將本質上採取當前應用程序活動的屏幕截圖,並在共享之前將其顯示在屏幕上,但當它到達屏幕時,它將耗盡內存在開始共享之前使用它來顯示屏幕截圖。Monodroid屏幕截圖內存

// image naming and path to include sd card appending name you choose for file 
Java.IO.File dir = new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures), "ScreenShots"); 

if (!dir.Exists()) 
    dir.Mkdirs(); 

// create bitmap screen capture 
Bitmap bitmap; 
View v1 = act.RootView; 
v1.DrawingCacheEnabled = true; 
bitmap = Bitmap.CreateBitmap(v1.DrawingCache); 
v1.DrawingCacheEnabled = false; 

string date = DateTime.Now.ToString(); 

date = date.Replace('/', '-'); 

Java.IO.File _file = new Java.IO.File(dir, from + date + ".png"); 
try 
{ 
    using (var stream = System.IO.File.Create(_file.AbsolutePath)) 
    { 
     bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, stream); 
     stream.Flush(); 
     stream.Close(); 
    } 
    bitmap.Dispose(); 
    v1.Dispose(); 
} 
catch (Java.IO.FileNotFoundException e) 
{ 
    // TODO Auto-generated catch block 
    e.PrintStackTrace(); 
} 
catch (Java.IO.IOException e) 
{ 
    // TODO Auto-generated catch block 
    e.PrintStackTrace(); 
} 

我試圖找到一個不犧牲太多質量的好方法。任何幫助,不勝感激=]

更新:我試着做這個反對上述沒有運氣。

View v1 = act.RootView; 
v1.DrawingCacheQuality = DrawingCacheQuality.Low; 
v1.DrawingCacheEnabled = true; 
Bitmap bitmap = Bitmap.CreateScaledBitmap(v1.GetDrawingCache(true),720,1280,false); 
v1.DrawingCacheEnabled = false; 
v1.DestroyDrawingCache(); 
v1.Dispose(); 

回答

0

我最終使用:

// create bitmap screen capture 
View v1 = act.RootView; 
Bitmap bitmap = Bitmap.CreateBitmap(720,1280,Bitmap.Config.Argb8888); 
Canvas can = new Canvas(bitmap); 
v1.Draw(can); 

比我更新的代碼,而。這有助於獲得我需要的屏幕截圖並減少內存使用量。