public static Decode ( |
||
codec | ||
Résultat |
public static Image LoadImage(byte[] pb) { if (pb == null) { return(null); } using (var ms = new MemoryStream(pb, false)) { return(Image.Decode(ms)); } }
public BitmapHelper(string path) { Bitmap bitmap; try { bitmap = Bitmap.Decode(File.OpenRead(path)); } catch (Exception) { bitmap = new Bitmap(10, 10); for (int i = 0; i < 10; i++) { for (int w = 0; w < 10; w++) { bitmap.SetPixel(i, w, SKColors.White); } } } IntPtr ptr = bitmap.GetPixels(); int bytes = Math.Abs(bitmap.RowBytes) * bitmap.Height; byte[] rgbValues = new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); colors = new SKColor[bitmap.Width, bitmap.Height]; for (int counter = 0; counter < rgbValues.Length; counter += 4) { int i_am = counter / 4; colors[i_am % bitmap.Width, i_am / bitmap.Width] = new SKColor( rgbValues[counter + 2], rgbValues[counter + 1], rgbValues[counter + 0], rgbValues[counter + 3]); } bitmap.Dispose(); }
public static void MagnifierImageFilter(SKCanvas canvas, int width, int height) { canvas.Clear(SKColors.White); var assembly = typeof(Demos).GetTypeInfo().Assembly; var imageName = assembly.GetName().Name + ".baboon.png"; var size = width > height ? height : width; var small = size / 5; // load the image from the embedded resource stream using (var resource = assembly.GetManifestResourceStream(imageName)) using (var stream = new SKManagedStream(resource)) using (var bitmap = SKBitmap.Decode(stream)) using (var filter = SKImageFilter.CreateMagnifier(SKRect.Create(small * 2, small * 2, small * 3, small * 3), small)) using (var paint = new SKPaint()) { paint.ImageFilter = filter; canvas.DrawBitmap(bitmap, SKRect.Create(width, height), paint); } }
public static void ColorMatrixColorFilter(SKCanvas canvas, int width, int height) { canvas.Clear(SKColors.White); var assembly = typeof(Demos).GetTypeInfo().Assembly; var imageName = assembly.GetName().Name + ".baboon.png"; // load the image from the embedded resource stream using (var resource = assembly.GetManifestResourceStream(imageName)) using (var stream = new SKManagedStream(resource)) using (var bitmap = SKBitmap.Decode(stream)) { var f = new Action <SKRect, float[]>((rect, colorMatrix) => { using (var cf = SKColorFilter.CreateColorMatrix(colorMatrix)) using (var paint = new SKPaint()) { paint.ColorFilter = cf; canvas.DrawBitmap(bitmap, rect, paint); } }); var colorMatrix1 = new float[20] { 0f, 1f, 0f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 1f, 0f }; var grayscale = new float[20] { 0.21f, 0.72f, 0.07f, 0.0f, 0.0f, 0.21f, 0.72f, 0.07f, 0.0f, 0.0f, 0.21f, 0.72f, 0.07f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f }; var colorMatrix3 = new float[20] { -1f, 1f, 1f, 0f, 0f, 1f, -1f, 1f, 0f, 0f, 1f, 1f, -1f, 0f, 0f, 0f, 0f, 0f, 1f, 0f }; var colorMatrix4 = new float[20] { 0.0f, 0.5f, 0.5f, 0f, 0f, 0.5f, 0.0f, 0.5f, 0f, 0f, 0.5f, 0.5f, 0.0f, 0f, 0f, 0.0f, 0.0f, 0.0f, 1f, 0f }; var highContrast = new float[20] { 4.0f, 0.0f, 0.0f, 0.0f, -4.0f * 255f / (4.0f - 1f), 0.0f, 4.0f, 0.0f, 0.0f, -4.0f * 255f / (4.0f - 1f), 0.0f, 0.0f, 4.0f, 0.0f, -4.0f * 255f / (4.0f - 1f), 0.0f, 0.0f, 0.0f, 1.0f, 0.0f }; var colorMatrix6 = new float[20] { 0f, 0f, 1f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 1f, 0f }; var sepia = new float[20] { 0.393f, 0.769f, 0.189f, 0.0f, 0.0f, 0.349f, 0.686f, 0.168f, 0.0f, 0.0f, 0.272f, 0.534f, 0.131f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f }; var inverter = new float[20] { -1f, 0f, 0f, 0f, 255f, 0f, -1f, 0f, 0f, 255f, 0f, 0f, -1f, 0f, 255f, 0f, 0f, 0f, 1f, 0f }; var matices = new[] { colorMatrix1, grayscale, highContrast, sepia, colorMatrix3, colorMatrix4, colorMatrix6, inverter }; var cols = width < height ? 2 : 4; var rows = (matices.Length - 1) / cols + 1; var w = (float)width / cols; var h = (float)height / rows; for (int y = 0; y < rows; y++) { for (int x = 0; x < cols; x++) { f(SKRect.Create(x * w, y * h, w, h), matices[y * cols + x]); } } } }