private void InternalImageSharpConvertImage(Stream inStream, Stream outStream, int width, int height, ThumbnailResizeType resizeType, ThumbnailFormatType formatType) { using var image = SixLabors.ImageSharp.Image.Load(inStream); image.Mutate(x => { var resizeOptions = new ResizeOptions { Position = AnchorPositionMode.Center, Size = new SixLabors.ImageSharp.Size(width, height), Mode = resizeType switch { ThumbnailResizeType.Pad => ResizeMode.Pad, ThumbnailResizeType.Crop => ResizeMode.Crop, _ => throw new NotSupportedException(), }, }; x.Resize(resizeOptions); }); if (formatType == ThumbnailFormatType.Png) { var encoder = new SixLabors.ImageSharp.Formats.Png.PngEncoder(); image.Save(outStream, encoder); return; } throw new NotSupportedException(); }
public byte[] ByteImageFromImageSharp(Image <Rgb24> image, int channels = 3) { image.Mutate(x => { SixLabors.ImageSharp.Size size = new SixLabors.ImageSharp.Size(Width, Height); x.Resize(size); }); int counter = 0; byte[] byte_image = new byte[image.Height * image.Width * channels]; for (int x = 0; x < image.Height; x++) { Span <Rgb24> pixelSpan = image.GetPixelRowSpan(x); for (int y = 0; y < image.Width; y++) { byte_image[counter] = pixelSpan[y].R; byte_image[counter + 1] = pixelSpan[y].G; byte_image[counter + 2] = pixelSpan[y].B; counter += 3; } } return(byte_image); }
private unsafe WriteableBitmap CreateBitmap() { WriteableBitmap writeableBitmap = null; FrameworkElement frameworkElement = mainGrid; if ( model != null && !double.IsNaN(frameworkElement.ActualWidth) && !double.IsNaN(frameworkElement.ActualHeight) && frameworkElement.ActualWidth != 0 && frameworkElement.ActualHeight != 0 ) { int pixelWidth = (int)frameworkElement.ActualWidth; int pixelHeight = (int)frameworkElement.ActualHeight; SixLabors.ImageSharp.Size size = new SixLabors.ImageSharp.Size(pixelWidth, pixelHeight); Matrix4D transform = DxfUtil.GetScaleTransform( bounds.Min, bounds.Max, bounds.Center, new Point3D(0, size.Height, 0), new Point3D(size.Width, 0, 0), new Point3D(0.5d * size.Width, 0.5d * size.Height, 0) ); writeableBitmap = new WriteableBitmap(pixelWidth, pixelHeight); cadGraphics.Draw(writeableBitmap, new Rectangle(0, 0, size.Width, size.Height), transform); } return(writeableBitmap); }
/// <summary> /// Calculates picture dimensions whilst maintaining aspect /// </summary> /// <param name="originalSize">The original picture size</param> /// <param name="targetSize">The target picture size (longest side)</param> /// <param name="resizeType">Resize type</param> /// <param name="ensureSizePositive">A value indicating whether we should ensure that size values are positive</param> /// <returns></returns> protected virtual Size CalculateDimensions(Size originalSize, int targetSize, ResizeType resizeType = ResizeType.LongestSide, bool ensureSizePositive = true) { float width, height; switch (resizeType) { case ResizeType.LongestSide: if (originalSize.Height > originalSize.Width) { // portrait width = originalSize.Width * (targetSize / (float)originalSize.Height); height = targetSize; } else { // landscape or square width = targetSize; height = originalSize.Height * (targetSize / (float)originalSize.Width); } break; case ResizeType.Width: width = targetSize; height = originalSize.Height * (targetSize / (float)originalSize.Width); break; case ResizeType.Height: width = originalSize.Width * (targetSize / (float)originalSize.Height); height = targetSize; break; default: throw new Exception("Not supported ResizeType"); } if (!ensureSizePositive) { return(new Size((int)Math.Round(width), (int)Math.Round(height))); } if (width < 1) { width = 1; } if (height < 1) { height = 1; } //we invoke Math.Round to ensure that no white background is rendered - https://www.nopcommerce.com/boards/topic/40616/image-resizing-bug return(new Size((int)Math.Round(width), (int)Math.Round(height))); }
public static void Create(Uri uri, string mime, int thumbnailSize, IFileInfo destination) { // Format is automatically detected though can be changed. var size = new SixLabors.ImageSharp.Size(thumbnailSize, thumbnailSize); using (var image = Image.Load(uri.LocalPath)) { image.Mutate(x => x.Resize(size.Width, size.Height)); // Automatic encoder selected based on extension. image.Save(destination.FullName); } }
public static ObjectDetectionDataset.Entry <float> YoloPreprocess(ObjectDetectionDataset.ClrEntry entry, Size targetSize) { if (entry.Image is null) { throw new ArgumentNullException(nameof(image)); } int h = entry.Image.Height, w = entry.Image.Width; float scale = Math.Min(targetSize.Width * 1f / w, targetSize.Height * 1f / h); int newW = (int)(scale * w), newH = (int)(scale * h); Resize(entry.Image, width: newW, height: newH); var padded = (ndarray <float>)np.full(shape: new[] { targetSize.Height, targetSize.Width, 3 }, fill_value: 128f, dtype: dtype.GetClass <float>()); int dw = (targetSize.Width - newW) / 2, dh = (targetSize.Height - newH) / 2; padded[dh..(newH + dh), dw..(newW + dw)] = entry.ToNumPyEntry().Image;