Skip to content

Commit def618e

Browse files
committed
Ability to set backgroundColor of a image
1 parent 48444a5 commit def618e

File tree

8 files changed

+52
-25
lines changed

8 files changed

+52
-25
lines changed

ImageFromXamarinUI/ImageFromXamarinUI.csproj

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,30 @@
1313
<AssemblyVersion>1.0.0.0</AssemblyVersion>
1414
<AssemblyFileVersion>1.0.0.0</AssemblyFileVersion>
1515
<Version>1.0.0</Version>
16-
<PackageVersion>1.0.0-pre1</PackageVersion>
16+
<PackageVersion>1.0.0-pre2</PackageVersion>
1717
<Authors>dimonovdd</Authors>
1818
<Owners>dimonovdd</Owners>
1919
<RepositoryUrl>https://github.com/dimonovdd/ImageFromXamarinUI</RepositoryUrl>
2020
<PackageReleaseNotes>See: https://github.com/dimonovdd/ImageFromXamarinUI/releases</PackageReleaseNotes>
2121
<DefineConstants>$(DefineConstants);</DefineConstants>
22-
<UseFullSemVerForNuGet>false</UseFullSemVerForNuGet>
2322
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
2423
<PackageLicenseFile>LICENSE</PackageLicenseFile>
2524
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
2625
<PackageProjectUrl>https://github.com/dimonovdd/ImageFromXamarinUI</PackageProjectUrl>
2726
<DebugType>portable</DebugType>
28-
<Configurations>Debug;Release</Configurations>
27+
<Configurations>Debug;Release</Configurations>
28+
<LangVersion>8.0</LangVersion>
2929
</PropertyGroup>
3030
<PropertyGroup Condition=" '$(Configuration)'=='Debug' ">
3131
<DebugSymbols>true</DebugSymbols>
3232
</PropertyGroup>
33-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
34-
<DocumentationFile>bin\Debug\netstandard2.0\ImageFromXamarinUI.xml</DocumentationFile>
35-
</PropertyGroup>
3633
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3734
<DocumentationFile>bin\Release\netstandard2.0\ImageFromXamarinUI.xml</DocumentationFile>
3835
</PropertyGroup>
3936
<ItemGroup>
4037
<None Include="..\LICENSE" PackagePath="" Pack="true" />
4138
<None Include="..\icon.png" PackagePath="" Pack="true" />
42-
<PackageReference Include="Xamarin.Build.TypeRedirector" Version="0.1.2-preview" PrivateAssets="all" />
39+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
4340
<PackageReference Include="Xamarin.Forms" Version="4.4.0.991864" />
4441
<Compile Include="**\*.shared.cs" />
4542
<Compile Include="**\*.shared.*.cs" />

ImageFromXamarinUI/VisualElementExtension.android.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ namespace ImageFromXamarinUI
99
{
1010
public static partial class VisualElementExtension
1111
{
12-
static async Task<Stream> PlatformCaptureImageAsync(VisualElement view)
12+
static async Task<Stream> PlatformCaptureImageAsync(VisualElement view, Xamarin.Forms.Color backgroundColor)
1313
{
14-
using var bitmap = ViewToBitMap(GetNativeView(view));
14+
using var bitmap = ViewToBitMap(GetNativeView(view), backgroundColor);
1515
var stream = await BitMapToStream(bitmap);
1616
bitmap.Recycle();
1717
return stream;
@@ -27,11 +27,11 @@ static Android.Views.View GetNativeView(VisualElement view)
2727
return render.View;
2828
}
2929

30-
static Bitmap ViewToBitMap(Android.Views.View view)
30+
static Bitmap ViewToBitMap(Android.Views.View view, Xamarin.Forms.Color backgroundColor)
3131
{
3232
var bitmap = Bitmap.CreateBitmap(view.Width, view.Height, Bitmap.Config.Argb8888);
3333
using var canvas = new Canvas(bitmap);
34-
canvas.DrawColor(Android.Graphics.Color.Transparent);
34+
canvas.DrawColor(backgroundColor.ToAndroid());
3535
view.Draw(canvas);
3636

3737
return bitmap;

ImageFromXamarinUI/VisualElementExtension.ios.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33
using System.Threading.Tasks;
4+
using CoreGraphics;
45
using UIKit;
56
using Xamarin.Forms;
67
using Xamarin.Forms.Platform.iOS;
@@ -9,10 +10,10 @@ namespace ImageFromXamarinUI
910
{
1011
public static partial class VisualElementExtension
1112
{
12-
static Task<Stream> PlatformCaptureImageAsync(VisualElement view)
13+
static Task<Stream> PlatformCaptureImageAsync(VisualElement view, Color backgroundColor)
1314
{
1415
Stream stream = null;
15-
using (var image = ViewToUIImage(GetNativeView(view)))
16+
using (var image = ViewToUIImage(GetNativeView(view), backgroundColor))
1617
stream = ImageToStream(image);
1718
return Task.FromResult(stream);
1819
}
@@ -27,10 +28,19 @@ static UIView GetNativeView(VisualElement view)
2728
return render.NativeView;
2829
}
2930

30-
static UIImage ViewToUIImage(UIView view)
31+
static UIImage ViewToUIImage(UIView view, Color backgroundColor)
3132
{
32-
UIGraphics.BeginImageContextWithOptions(view.Frame.Size, false, UIScreen.MainScreen.Scale);
33-
view.Layer.RenderInContext(UIGraphics.GetCurrentContext());
33+
var size = view.Frame.Size;
34+
UIGraphics.BeginImageContextWithOptions(size, false, UIScreen.MainScreen.Scale);
35+
using var context = UIGraphics.GetCurrentContext();
36+
37+
if(backgroundColor != Color.Transparent)
38+
{
39+
context.SetFillColor(backgroundColor.ToCGColor());
40+
context.FillRect(new CGRect(0, 0, size.Width, size.Height));
41+
}
42+
43+
view.Layer.RenderInContext(context);
3444
var image = UIGraphics.GetImageFromCurrentImageContext();
3545
UIGraphics.EndImageContext();
3646

ImageFromXamarinUI/VisualElementExtension.netstandard.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace ImageFromXamarinUI
77
{
88
public static partial class VisualElementExtension
99
{
10-
static Task<Stream> PlatformCaptureImageAsync(VisualElement view)
10+
static Task<Stream> PlatformCaptureImageAsync(VisualElement view, Color backgroundColor)
1111
=> throw new NotImplementedException($"{nameof(CaptureImageAsync)} not supported on netstandart project");
1212

1313
}

ImageFromXamarinUI/VisualElementExtension.shared.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
namespace ImageFromXamarinUI
66
{
7+
/// <summary>The extension class with methods for creating images</summary>
78
public static partial class VisualElementExtension
89
{
910
/// <summary>Captures an image from the current state of <paramref name="element"/></summary>
1011
/// <param name="element">element to which the render was assigned</param>
12+
/// <param name="backgroundColor"></param>
1113
/// <returns>Stream an image as png with transparency</returns>
12-
public static async Task<Stream> CaptureImageAsync(this VisualElement element)
13-
=> await PlatformCaptureImageAsync(element);
14+
public static async Task<Stream> CaptureImageAsync(this VisualElement element, Color? backgroundColor = null)
15+
=> await PlatformCaptureImageAsync(element, backgroundColor ?? Color.Transparent);
1416
}
1517
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async void OnCapture(Xamarin.Forms.VisualElement element)
2727
{
2828
try
2929
{
30-
var stream = await element.CaptureImageAsync();
30+
var stream = await element.CaptureImageAsync(Color.White);
3131
ResultImageSource = ImageSource.FromStream(() => stream);
3232
}
3333
catch (Exception)

Sample/Sample/MainPage.xaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<sample:MainViewModel />
1111
</ContentPage.BindingContext>
1212
<ScrollView >
13-
<StackLayout Background="Red" >
13+
<StackLayout Background="Green" >
1414
<Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
1515
<Label Text="Welcome to Xamarin.Forms!" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
1616
</Frame>
@@ -27,8 +27,11 @@
2727
</Label.FormattedText>
2828
</Label>
2929

30-
<Button Text="Capture" Background="#990000ff"
31-
Command="{Binding CaptureCommand}" CommandParameter="{x:Reference rootView}"/>
30+
<ContentView x:Name="button">
31+
<Button Text="Capture" Background="#990000ff" Margin="10"
32+
Command="{Binding CaptureCommand}" CommandParameter="{x:Reference button}"/>
33+
</ContentView>
34+
3235

3336
<Image Source="{Binding ResultImageSource}"/>
3437

Sample/Sample/MainViewModel.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using System.Windows.Input;
1+
using System.IO;
2+
using System.Windows.Input;
23
using ImageFromXamarinUI;
4+
using Xamarin.Essentials;
35
using Xamarin.Forms;
46

57
namespace Sample
@@ -27,8 +29,21 @@ public ImageSource ResultImageSource
2729

2830
async void OnCapture(VisualElement element)
2931
{
30-
var stream = await element.CaptureImageAsync();
32+
var stream = await element.CaptureImageAsync(Color.Red.MultiplyAlpha(0.4));
3133
ResultImageSource = ImageSource.FromStream(() => stream);
34+
35+
36+
//var dir = FileSystem.CacheDirectory;
37+
//var filepath = Path.Combine(dir, "image.jpg");
38+
39+
//if (File.Exists(filepath))
40+
// File.Delete(filepath);
41+
42+
//var fileStream = File.OpenWrite(filepath);
43+
//stream.CopyTo(fileStream);
44+
//fileStream.Close();
45+
46+
//await Share.RequestAsync(new ShareFileRequest(new ShareFile(filepath)));
3247
}
3348
}
3449
}

0 commit comments

Comments
 (0)