Skip to content

Instantly share code, notes, and snippets.

Avatar

Atsushi Nakamura nuitsjp

View GitHub Profile
@nuitsjp
nuitsjp / BitmapSourceExtensions.cs
Created Oct 17, 2016
BitmapSource to expand while maintaining the DPI.
View BitmapSourceExtensions.cs
public static BitmapSource Resize(this BitmapSource source, int resolution)
{
var scale = resolution / source.DpiX;
// サイズ変更
var transformedBitmap = new TransformedBitmap();
transformedBitmap.BeginInit();
transformedBitmap.Source = source;
transformedBitmap.Transform = new ScaleTransform(scale, scale);
transformedBitmap.EndInit();
View BitmapSourceExtensions.cs
public static BitmapSource Resize(this BitmapSource source, int resolution)
{
var scale = resolution / source.DpiX;
// サイズ変更
var transformedBitmap = new TransformedBitmap();
transformedBitmap.BeginInit();
transformedBitmap.Source = source;
transformedBitmap.Transform = new ScaleTransform(scale, scale);
transformedBitmap.EndInit();
View BitmapSourceExtensions.cs
public static BitmapSource Rotation(this BitmapSource source, int angle)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var transformedBitmap = new TransformedBitmap();
transformedBitmap.BeginInit();
transformedBitmap.Source = source;
transformedBitmap.Transform = new RotateTransform(90);
transformedBitmap.EndInit();
return transformedBitmap;
@nuitsjp
nuitsjp / BitmapSourceExtensions.cs
Created Oct 17, 2016
Save to Jpeg from BitmapSource.
View BitmapSourceExtensions.cs
public static byte[] SaveToJpeg(this BitmapSource source, int qualityLevel)
{
if (source == null) throw new ArgumentNullException(nameof(source));
using (var outputStream = new MemoryStream())
{
// WICのEncoderを利用してJpegファイルに保存する
// System.Drawingパッケージの場合、8bit形式のJpegに対応していないため
var encoder = new JpegBitmapEncoder { QualityLevel = qualityLevel };
encoder.Frames.Add(BitmapFrame.Create(source));
@nuitsjp
nuitsjp / BitmapImageFactory.cs
Created Oct 17, 2016
Create BitmapSource by specifying a format from the byte sequence
View BitmapImageFactory.cs
public static BitmapSource CreateFormJpeg(byte[] bytes)
{
using (var stream = new MemoryStream(bytes))
{
var decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
return decoder.Frames[0];
}
}
@nuitsjp
nuitsjp / BitmapImageFactory.cs
Created Oct 17, 2016
Create BitmapImage from byte array.
View BitmapImageFactory.cs
public static BitmapImage Create(byte[] bytes)
{
var result = new BitmapImage();
using (var stream = new MemoryStream(bytes))
{
result.BeginInit();
result.CacheOption = BitmapCacheOption.OnLoad;
result.CreateOptions = BitmapCreateOptions.None;
result.StreamSource = stream;
@nuitsjp
nuitsjp / App.xaml.cs
Created Aug 21, 2016
NavigationService Overview App.xaml.cs
View App.xaml.cs
protected override void OnInitialized()
{
InitializeComponent();
//NavigationService.NavigateAsync("MainPage?title=Hello%20from%20Xamarin.Forms");
NavigationService.NavigateAsync("NavigationPage/MainPage?title=Hello%20from%20Xamarin.Forms");
}
protected override void RegisterTypes()
{
@nuitsjp
nuitsjp / SecondPage2.xaml
Created Aug 21, 2016
NavigationService Overview SecondPage2.xaml
View SecondPage2.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="NavigationService.Views.SecondPage">
<StackLayout Orientation="Vertical" HorizontalOptions="Center" VerticalOptions="Center">
<Label Text="Second Page."/>
<Button Text="Go Back." Command="{Binding GoBackCommand}"/>
</StackLayout>
@nuitsjp
nuitsjp / SecondPageViewModel2.cs
Created Aug 21, 2016
NavigationService Overview SecondPageViewModel2.cs
View SecondPageViewModel2.cs
public class SecondPageViewModel : BindableBase
{
private readonly INavigationService _navigationService;
public ICommand GoBackCommand { get; }
public SecondPageViewModel(INavigationService navigationService)
{
_navigationService = navigationService;
GoBackCommand = new DelegateCommand(() =>
{
_navigationService.GoBackAsync();
@nuitsjp
nuitsjp / MainPage2.xaml
Last active Aug 21, 2016
NavigationService Overview MainPage2.xaml
View MainPage2.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="NavigationService.Views.MainPage"
Title="MainPage">
<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
<Label Text="{Binding Title}" />
<Button Text="Navigate Second Page." Command="{Binding NavigateSecondCommand}"/>