Skip to content

Instantly share code, notes, and snippets.

View nuitsjp's full-sized avatar

Atsushi Nakamura nuitsjp

View GitHub Profile
@nuitsjp
nuitsjp / BitmapExtensions.cs
Created October 17, 2016 07:58
Convert System.Drawing.Bitmap to System.Windows.Media.ImageSource
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteObject([In] IntPtr hObject);
public static ImageSource ToImageSource(this Bitmap bmp)
{
var handle = bmp.GetHbitmap();
try
{
return Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
@nuitsjp
nuitsjp / BitmapSourceExtensions.cs
Created October 17, 2016 02:15
BitmapSource to expand while maintaining the DPI.
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();
@nuitsjp
nuitsjp / BitmapSourceExtensions.cs
Created October 17, 2016 02:12
Rescale BitmapSource
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();
@nuitsjp
nuitsjp / BitmapSourceExtensions.cs
Created October 17, 2016 02:11
Rotation BitmapSource
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 October 17, 2016 01:58
Save to Jpeg from BitmapSource.
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 October 17, 2016 01:52
Create BitmapSource by specifying a format from the byte sequence
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 October 17, 2016 01:27
Create BitmapImage from byte array.
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 August 21, 2016 07:06
NavigationService Overview 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 August 21, 2016 06:27
NavigationService Overview 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 August 21, 2016 06:25
NavigationService Overview 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();