How to create bitmap from string text with SkiaSharp - xamarin

I created an entry where you type in a value, but now I want to create a bitmap with the text of an entry. When I got the bitmap, I can load it in SkiaSharp and use BitmapModifiers on it.
Is this possible?

I would recommend you to go through these examples on github.
Dummy sample could look like this:
This is xaml page:
<StackLayout>
<Entry x:Name="InputText" />
<Button Text="Create Bitmap" Clicked="Button_Clicked" />
</StackLayout>
Codebehind:
SKBitmap helloBitmap;
void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
{
SKImageInfo info = args.Info;
SKSurface surface = args.Surface;
SKCanvas canvas = surface.Canvas;
canvas.Clear(SKColors.Aqua);
canvas.DrawBitmap(helloBitmap, 0, 0);
}
private void Button_Clicked(object sender, EventArgs e)
{
CreateBitmapFromText(InputText.Text);
}
private void CreateBitmapFromText(string text)
{
// Create bitmap and draw on it
using (SKPaint textPaint = new SKPaint { TextSize = 48 })
{
SKRect bounds = new SKRect();
textPaint.MeasureText(text, ref bounds);
helloBitmap = new SKBitmap((int)bounds.Right,
(int)bounds.Height);
using (SKCanvas bitmapCanvas = new SKCanvas(helloBitmap))
{
bitmapCanvas.Clear();
bitmapCanvas.DrawText(text, 0, -bounds.Top, textPaint);
}
}
SKCanvasView canvasView = new SKCanvasView();
canvasView.PaintSurface += OnCanvasViewPaintSurface;
Content = canvasView;
}

Related

I want to emit a shutter sound when the camera shutter button is pressed by Xamarin.Form

I am building a camera application in Xamarin.Forms with a custom renderer.
Android.
I want to emit a shutter sound when the camera shutter button is pressed.
Currently, I have the TalkePhotoButtonClickedHandler as shown below.
private async void TakePhotoButtonClickedHandler(object sender, EventArgs e)
{
if (this.imageAvailableListener == null)
{
this.imageAvailableListener = new ImageAvailableListener(this);
}
var bitmap = this.texture.Bitmap;
CameraManager manager = (CameraManager)MainActivity.context.GetSystemService(Context.CameraService);
try
{
CameraCharacteristics characteristics = manager.GetCameraCharacteristics(this.device.Id);
StreamConfigurationMap map = characteristics.Get(CameraCharacteristics.ScalerStreamConfigurationMap) as StreamConfigurationMap;
Size[]? jpegSize = map.GetOutputSizes((int)ImageFormatType.Jpeg);
int ww = jpegSize[0].Width;
int hh = jpegSize[0].Height;
ImageReader reader = ImageReader.NewInstance(ww, hh, ImageFormatType.Jpeg, 1);
List<Surface> outputSurfaces = new List<Surface>(2);
outputSurfaces.Add(reader.Surface);
outputSurfaces.Add(new Surface(this.texture.SurfaceTexture));
CaptureRequest.Builder captureBuilder = this.device.CreateCaptureRequest(CameraTemplate.StillCapture);
captureBuilder.AddTarget(reader.Surface);
captureBuilder.Set(CaptureRequest.ControlMode, (int)ControlMode.Auto);
reader.SetOnImageAvailableListener(imageAvailableListener, backgroundHandler);
var sessionListener = new CameraSessionCallback(this);
this.device.CreateCaptureSession(
outputSurfaces,
new CameraStateCallback(captureBuilder, sessionListener, this.backgroundHandler),
this.backgroundHandler
);
}
catch (CameraAccessException ex)
{
throw ex;
}
}
It looks like I should write the shutter callback here.
private Camera.ShutterCallback mShutterListener =
new Camera.ShutterCallback() {
public void onShutter() {
// empty OK。
}
};
private Camera.PictureCallback mPictureListener =
new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
}
};
I found the Java code, but how can I write it in C#?
I use Xamarin.Essentials: Media Picker to take photo, a shutter sound when the camera shutter button is pressed.
<StackLayout>
<Image x:Name="PhotoImage" />
<Button
x:Name="takephoto"
Clicked="takephoto_Clicked"
Text="take photo" />
</StackLayout>
private async void takephoto_Clicked(object sender, EventArgs e)
{
var result = await Xamarin.Essentials.MediaPicker.CapturePhotoAsync();
if (result != null)
{
var stream = await result.OpenReadAsync();
takephoto.ImageSource = ImageSource.FromStream(() => stream);
}
}

How to save the final photo in Forms.Image to local in xamarin forms

I'm making an app for android with Xamarin forms where the user would select a photo from their library, add a White border to make it a square, then save it. I'm stuck at the saving part, can't figure it out
I am using the normal Xamarin.Forms.Image control
<Image HorizontalOptions="CenterAndExpand"
VerticalOptions="Center"
BackgroundColor="Transparent"
Grid.Column="0"
Grid.Row="0"
x:Name="imgViewer"/>
this is how I select the photo
async void tbAdd_Activated(object sender, System.EventArgs e)
{
var file = await CrossFilePicker.Current.PickFile();
if (file != null)
{
imgViewer.Source = ImageSource.FromFile(file.FilePath);
imgViewer.BackgroundColor = Color.White;
}
}
But then I have a save button to save that final image with the border to the camera roll but I have no idea how to save it, I've searched everywhere but can't seem to find!
Anyone knows how to do that ?
You will have to specific codes in iOS and android to save the images and Also add required permissions in iOS by adding this key to info.Plist file
<key>NSPhotoLibraryAddUsageDescription</key>
<string>$(PRODUCT_NAME)</string>
Android :
MediaStore.Images.Media.InsertImage(Application.Context.ContentResolver, [YourFileName],
System.IO.Path.GetFileName([YourFileName]), string.Empty);
iOS :
var imageSource = CGImageSource.FromUrl(NSUrl.FromFilename( [YourFileName]));
UIImage.FromImage(imageSource.CreateImage(0, null), 1, imageOrientation)
.SaveToPhotosAlbum((image, error) => { // handle success & error here... });
You can check this link for more info.
You can save the image by the following code:
void save_Activated(object sender, System.EventArgs e)
{
if (file != null)
{
byte[] imageBytes;
var FileImage = new Image();
string base64;
using (var fs = new FileStream(file.FilePath, FileMode.Open, FileAccess.Read))
{
var buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
base64 = Convert.ToBase64String(buffer);
}
imageBytes = Convert.FromBase64String(base64);
File.WriteAllBytes(filename, imageBytes);
}
So the full code is like this:
string documentsFolder;
string filename;
FileData file;
ImageSource imageSource;
public MainPage()
{
InitializeComponent();
documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
filename = Path.Combine(documentsFolder, "myfile.jpg");
}
async void tbAdd_Activated(object sender, System.EventArgs e)
{
file = await CrossFilePicker.Current.PickFile();
if (file != null)
{
imgViewer.Source = ImageSource.FromFile(file.FilePath);
imgViewer.BackgroundColor = Color.White;
imageSource = imgViewer.Source;
}
}
void save_Activated(object sender, System.EventArgs e)
{
if (file != null)
{
byte[] imageBytes;
var FileImage = new Image();
string base64;
using (var fs = new FileStream(file.FilePath, FileMode.Open, FileAccess.Read))
{
var buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
base64 = Convert.ToBase64String(buffer);
}
imageBytes = Convert.FromBase64String(base64);
File.WriteAllBytes(filename, imageBytes);
}
}
And the xamal is:
<Button Text="AddImage" Clicked="tbAdd_Activated">
</Button>
<Button Text="SaveImage" Clicked="save_Activated">
</Button>
<Image HorizontalOptions="CenterAndExpand"
VerticalOptions="Center"
BackgroundColor="Transparent"
Grid.Column="0"
Grid.Row="0"
x:Name="imgViewer"/>

Changing the image on a button when clicked xaml

im trying to change between two images of a button when the same button is clicked
my xaml code is
<Button x:Name="BidderOne"
Click="BidderOne_Click" Height="5"
Grid.Row="2"
BorderBrush="#FFE7E3E3"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="350,0,0,280"
>
<Button.Template>
<ControlTemplate>
<Image x:Name="BidderOneImage"
Source="/Assets/Star.png"
Width="50"/>
</ControlTemplate>
</Button.Template>
</Button>
im not sure what to write in my class, but what i found was something along the lines of this
private void BidderOne_Click(object sender, RoutedEventArgs e)
{
var selectedBidder = sender as Button;
// Button btn = new Button();
// btn = BidderOne;
ControlTemplate dt = BidderOne.Template;
Image btnImage = (Image)dt.TargetType = ;
var img = (Image)(selectedBidder.ContentTemplateRoot as StackPanel).Children[0];
var uri = new Uri("/Assetes/ClubsLogo.png", UriKind.Relative);
// var sri = Windows.UI.Xaml.Application.("Assetes/ClubsLogo.png", UriKind.Relative);
imgOn.UriSource=uri;
img.Source = imgOn;
}
It is not recommended to change the ControlTemplate of a Control at runtime. For these situations you should create a custom control.
I have written a simple ImageButton just so you can get the idea of creating what is needed for your problem.
Here is the code:
public sealed class ImageButton : Button
{
private Image _image;
public ImageButton()
{
this.DefaultStyleKey = typeof (Button);
_image = new Image();
}
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.Content = _image;
OnImage1SourceChanged(null, null);
}
public ImageSource Image1Source
{
get { return (ImageSource)GetValue(Image1SourceProperty); }
set { SetValue(Image1SourceProperty, value); }
}
public static readonly DependencyProperty Image1SourceProperty =
DependencyProperty.Register("Image1Source",
typeof (ImageSource),
typeof (ImageButton),
new PropertyMetadata(null, OnImage1SourceChangedCallback));
private static void OnImage1SourceChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var imageButton = d as ImageButton;
if (imageButton == null) return;
imageButton.OnImage1SourceChanged((ImageSource)e.OldValue, (ImageSource)e.NewValue);
}
private void OnImage1SourceChanged(ImageSource oldValue, ImageSource newValue)
{
if (_image != null)
{
_image.Source = Image1Source;
}
}
public ImageSource Image2Source
{
get { return (ImageSource)GetValue(Image2SourceProperty); }
set { SetValue(Image2SourceProperty, value); }
}
public static readonly DependencyProperty Image2SourceProperty =
DependencyProperty.Register("Image2Source",
typeof (ImageSource),
typeof (ImageButton),
new PropertyMetadata(null));
protected override void OnTapped(TappedRoutedEventArgs e)
{
base.OnTapped(e);
if (_image != null)
{
_image.Source = Image2Source;
}
}
}
and you use it like this:
<controls:ImageButton Width="60" Height="60" Image1Source="/Assets/Images/Chat/ic_comment_favorite_yellow.png" Image2Source="/Assets/Images/Flags/afghanistan.png"/>
Try this
EDITED
private void BidderOne_Click(object sender, RoutedEventArgs e) {
var selectedBidder = sender as Button;
// WRONG -> Image image = selectedBidder.Template.FindName("BidderOneImage", selectedBidder) as Image;
Image image = VisualTreeHelper.GetChild(selectedBidder, 0) as Image;
image.Source = new BitmapImage(new Uri("NEW IMAGE URI"));
}

Expanding the size of ListView cell on tap

I'm trying to implement a solution to increase the size of a ListView Cell when tapped using Xamarin Forms (and custom renderers if required).
I'm still pretty new to C#, and the idea of data binding is still a little unclear to me, however, it seems like that is the way to go to solve this problem (perhaps something along the lines of binding the Height / HeightRequest properties of the cell?).
My attempts thus far have been unsuccessful.
If anyone could give me a push in the right direction it would be much appreciated.
Thank you!
ViewCell does not expose Height as a BindableProperty in Xamarin.Forms 1.4.2x
However if you create your own BindableProperty in your Model you can achieve changing the height still as shown below:-
Model:-
public class MenuItem2 : BindableObject
{
public static readonly BindableProperty TextProperty = BindableProperty.Create<MenuItem2, string>(p => p.Text, default(string));
public static readonly BindableProperty CellHeightProperty = BindableProperty.Create<MenuItem2, int>(p => p.CellHeight, default(int));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public int CellHeight
{
get { return (int)GetValue(CellHeightProperty); }
set { SetValue(CellHeightProperty, value); }
}
}
XAML:-
<StackLayout>
<Button x:Name="cmdButton1" Text="Change Cell Heights" Clicked="cmdButton1_Clicked"/>
<ListView x:Name="lstItems" />
</StackLayout>
XAML Code-Behind:-
lstItems.HasUnevenRows = true;
lstItems.ItemTemplate = new DataTemplate(typeof(Classes.MenuCell2));
//
lstItems.ItemsSource = new List<MenuItem2>
{
new MenuItem2(),
new MenuItem2(),
new MenuItem2(),
new MenuItem2(),
};
If you don't set .HasUnevenRows you will not be able to change the cell height.
void cmdButton1_Clicked(object sender, EventArgs e)
{
Random objRandom = new Random();
//
var objItems = lstItems.ItemsSource;
//
foreach (MenuItem2 objMenuItem in objItems)
{
int intNewCellHeight = objRandom.Next(80, 160);
objMenuItem.CellHeight = intNewCellHeight;
objMenuItem.Text = "Cell Height = " + intNewCellHeight.ToString();
}
}
Custom ViewCell:-
public class MenuCell2 : ViewCell
{
public MenuCell2()
{
Label objLabel = new Label
{
YAlign = TextAlignment.Center,
TextColor = Color.Yellow,
};
objLabel.SetBinding(Label.TextProperty, new Binding("Text"));
StackLayout objLayout = new StackLayout
{
Padding = new Thickness(20, 0, 0, 0),
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.StartAndExpand,
Children = { objLabel }
};
Frame objFrame_Inner = new Frame
{
Padding = new Thickness(15, 15, 15, 15),
HeightRequest = 36,
OutlineColor = Color.Accent,
BackgroundColor = Color.Blue,
Content = objLayout,
};
Frame objFrame_Outer = new Frame
{
Padding = new Thickness(0, 0, 0, 10),
Content = objFrame_Inner
};
View = objFrame_Outer;
this.BindingContextChanged += MenuCell2_BindingContextChanged;
}
void MenuCell2_BindingContextChanged(object sender, EventArgs e)
{
MenuItem2 objMenuItem = (MenuItem2)this.BindingContext;
objMenuItem.PropertyChanged += objMenuItem_PropertyChanged;
}
void objMenuItem_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "CellHeight":
this.Height = (this.BindingContext as MenuItem2).CellHeight;
(this.View as Frame).ForceLayout();
break;
}
}
Remember to call ForceLayout on the root element of the ViewCell's View property, so it can redraw correctly.
This will give you a result something similar to the following (tested only on WindowsPhone at present):-
In order to do it on a ViewCell being tapped, on the XAML Page add:-
lstItems.ItemTapped += lstItems_ItemTapped;
and then change the model for the item to something like this:-
void lstItems_ItemTapped(object sender, ItemTappedEventArgs e)
{
(e.Item as MenuItem2).CellHeight = 200;
}
Xamarin now has an official example of doing this right within xaml and xaml code behind:
Overview:
https://developer.xamarin.com/samples/xamarin-forms/UserInterface/ListView/DynamicUnevenListCells/
Code:
https://github.com/xamarin/xamarin-forms-samples/tree/master/UserInterface/ListView/DynamicUnevenListCells

how save Grid Background in my app

how seve the grid background after choose image by choose image task
EX:
private void photoChooserTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(e.ChosenPhoto);
ImageBrush img = new ImageBrush();
img.ImageSource = bmp;
LayoutRoot.Background = img;
//Save grid Background
}
}
help me please
thank you :)
Note: 'Save' means that when the application open next time
be 'grid backgrund' the same background chosen
Try this.. In the example i have used a button to activate the PhotoChooserTask. The XAML is like this.
XAML
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Name="btnSet" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Content="Set Image" Click="btnSet_Click" />
</Grid>
</Grid>
Click event for the button.
C#
private void btnSet_Click(object sender, RoutedEventArgs e)
{
PhotoChooserTask photoTask = new PhotoChooserTask();
photoTask.Completed += photoTask_Completed;
photoTask.PixelHeight = 1280;
photoTask.PixelWidth = 768;
photoTask.Show();
}
In the photoTask_Completed event handler you can save the image to the IsolatedStorage
void photoTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isoStore.FileExists(fileName))
{
isoStore.DeleteFile(fileName);
}
using (IsolatedStorageFileStream targetStream = isoStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
{
byte[] readBuffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = e.ChosenPhoto.Read(readBuffer, 0, readBuffer.Length)) > 0)
{
targetStream.Write(readBuffer, 0, bytesRead);
}
}
}
}
}
Then in the OnNavigatedTo event you can load the image from IsolatedStorage and set it as the background.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
byte[] data;
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isoStore.FileExists(fileName))
{
using(IsolatedStorageFileStream stream = isoStore.OpenFile(fileName, FileMode.Open, FileAccess.Read))
{
data = new byte[stream.Length];
stream.Read(data, 0, data.Length);
}
MemoryStream ms = new MemoryStream(data);
bmp = new BitmapImage();
bmp.SetSource(ms);
ImageBrush img = new ImageBrush();
img.ImageSource = bmp;
LayoutRoot.Background = img;
}
}
}
The variable fileName holds the name of the image saved to the IsolatedStorage. The image is overwritten every time you select a new image from the library. Hope this helps.
try this:
XAML
<Grid x:Name="LayoutRoot">
<Grid.Background>
<ImageBrush x:Name="imgsrc"></ImageBrush>
</Grid.Background>
</Grid>
CS:
if (e.TaskResult == TaskResult.OK)
{
BitmapImage Bitmap = new BitmapImage();
Bitmap.SetSource(e.ChosenPhoto);
imgsrc.ImageSource = Bitmap;
}
for Saving image you would be needed to use isolatedstorage. You would be needed to save your image in Isolatedstorage and have a isolatedsettings variabe for saving the state of the image whether you have choosen any image or not.
If yes then get image from there otherwise no action would be needed, here is a good example from you can have reference for saving and Retriving image from Isolatedstorage Isolated Storage - Read and Save Images

Resources