-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CaptureImageAsync method implemented
- Loading branch information
Showing
22 changed files
with
2,828 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 19 additions & 16 deletions
35
...wToImage/Xamarin.Forms.ViewToImage.csproj → ImageFromXamarinUI/ImageFromXamarinUI.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Android.Graphics; | ||
using Xamarin.Forms; | ||
using Xamarin.Forms.Platform.Android; | ||
|
||
namespace ImageFromXamarinUI | ||
{ | ||
public static partial class VisualElementExtension | ||
{ | ||
static async Task<Stream> PlatformCaptureImageAsync(VisualElement view) | ||
{ | ||
using var bitmap = ViewToBitMap(GetNativeView(view)); | ||
var stream = await BitMapToStream(bitmap); | ||
bitmap.Recycle(); | ||
return stream; | ||
} | ||
|
||
static Android.Views.View GetNativeView(VisualElement view) | ||
{ | ||
var render = Platform.GetRenderer(view); | ||
|
||
if (render == null) | ||
throw new ArgumentException($"The {view} parameter does not have a render", nameof(view)); | ||
|
||
return render.View; | ||
} | ||
|
||
static Bitmap ViewToBitMap(Android.Views.View view) | ||
{ | ||
var bitmap = Bitmap.CreateBitmap(view.Width, view.Height, Bitmap.Config.Argb8888); | ||
using var canvas = new Canvas(bitmap); | ||
canvas.DrawColor(Android.Graphics.Color.Transparent); | ||
view.Draw(canvas); | ||
|
||
return bitmap; | ||
} | ||
|
||
static async Task<Stream> BitMapToStream(Bitmap bitmap) | ||
{ | ||
var ms = new MemoryStream(); | ||
await bitmap.CompressAsync(Bitmap.CompressFormat.Png, 100, ms).ConfigureAwait(false); | ||
ms.Position = 0; | ||
return ms; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using UIKit; | ||
using Xamarin.Forms; | ||
using Xamarin.Forms.Platform.iOS; | ||
|
||
namespace ImageFromXamarinUI | ||
{ | ||
public static partial class VisualElementExtension | ||
{ | ||
static Task<Stream> PlatformCaptureImageAsync(VisualElement view) | ||
{ | ||
Stream stream = null; | ||
using (var image = ViewToUIImage(GetNativeView(view))) | ||
stream = ImageToStream(image); | ||
return Task.FromResult(stream); | ||
} | ||
|
||
static UIView GetNativeView(VisualElement view) | ||
{ | ||
var render = Platform.GetRenderer(view); | ||
|
||
if (render == null) | ||
throw new ArgumentException($"The {view} parameter does not have a render", nameof(view)); | ||
|
||
return render.NativeView; | ||
} | ||
|
||
static UIImage ViewToUIImage(UIView view) | ||
{ | ||
UIGraphics.BeginImageContextWithOptions(view.Frame.Size, false, UIScreen.MainScreen.Scale); | ||
view.Layer.RenderInContext(UIGraphics.GetCurrentContext()); | ||
var image = UIGraphics.GetImageFromCurrentImageContext(); | ||
UIGraphics.EndImageContext(); | ||
|
||
return image; | ||
} | ||
|
||
static Stream ImageToStream(UIImage image) | ||
{ | ||
var imageData = image.AsPNG(); | ||
return imageData.AsStream(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Xamarin.Forms; | ||
|
||
namespace ImageFromXamarinUI | ||
{ | ||
public static partial class VisualElementExtension | ||
{ | ||
static Task<Stream> PlatformCaptureImageAsync(VisualElement view) | ||
=> throw new NotImplementedException($"{nameof(CaptureImageAsync)} not supported on netstandart project"); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Xamarin.Forms; | ||
|
||
namespace ImageFromXamarinUI | ||
{ | ||
public static partial class VisualElementExtension | ||
{ | ||
/// <summary>Captures an image from the current state of <paramref name="element"/></summary> | ||
/// <param name="element">element to which the render was assigned</param> | ||
/// <returns>Stream an image as png with transparency</returns> | ||
public static async Task<Stream> CaptureImageAsync(this VisualElement element) | ||
=> await PlatformCaptureImageAsync(element); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
Sample.Xamarin.Forms.ViewToImage/ViewExtension.netstandard.cs
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.