public void Create(Stream stream) { Control = new NSImage(NSData.FromStream(stream)); rep = Control.BestRepresentationForDevice(null); bmprep = rep as NSBitmapImageRep; Control.Size = new CGSize(rep.PixelsWide, rep.PixelsHigh); }
public BitmapHandler(NSImage image) { Control = image; rep = GetBestRepresentation(); bmprep = rep as NSBitmapImageRep; alpha = rep.HasAlpha; }
public static Gdk.Pixbuf GetPixbufFromNSImageRep(NSImageRep rep, int width, int height) { var rect = new CGRect(0, 0, width, height); var bitmap = rep as NSBitmapImageRep; try { if (bitmap == null) { using (var cgi = rep.AsCGImage(ref rect, null, null)) { if (cgi == null) { return(null); } bitmap = new NSBitmapImageRep(cgi); } } return(GetPixbufFromNSBitmapImageRep(bitmap, width, height)); } finally { if (bitmap != null) { bitmap.Dispose(); } } }
protected void EnsureRep() { if (rep == null) { rep = Control.BestRepresentationForDevice(null); } if (bmprep != null) { return; } if (rep is IconFrameHandler.LazyImageRep lazyRep) { bmprep = lazyRep.Rep; } else { bmprep = rep as NSBitmapImageRep ?? Control.BestRepresentationForDevice(null) as NSBitmapImageRep; } if (bmprep == null) { Control.LockFocus(); bmprep = new NSBitmapImageRep(new CGRect(CGPoint.Empty, Control.Size)); //bmprep.Type.ColorSpaceName = Control.Representations()[0].ColorSpaceName; Control.UnlockFocus(); } }
public static Gdk.Pixbuf GetPixbufFromNSImageRep(NSImageRep rep, int width, int height) { var rect = new System.Drawing.RectangleF(0, 0, width, height); var bitmap = rep as NSBitmapImageRep; if (bitmap == null) { using (var cgi = rep.AsCGImage(rect, null, null)) bitmap = new NSBitmapImageRep(cgi); } try { byte[] data; using (var tiff = bitmap.TiffRepresentation) { data = new byte[tiff.Length]; System.Runtime.InteropServices.Marshal.Copy(tiff.Bytes, data, 0, data.Length); } int pw = bitmap.PixelsWide, ph = bitmap.PixelsHigh; var pixbuf = new Gdk.Pixbuf(data, pw, ph); // if one dimension matches, and the other is same or smaller, use as-is if ((pw == width && ph <= height) || (ph == height && pw <= width)) { return(pixbuf); } // otherwise scale proportionally such that the largest dimension matches the desired size if (pw == ph) { pw = width; ph = height; } else if (pw > ph) { ph = (int)(width * ((float)ph / pw)); pw = width; } else { pw = (int)(height * ((float)pw / ph)); ph = height; } var scaled = pixbuf.ScaleSimple(pw, ph, Gdk.InterpType.Bilinear); pixbuf.Dispose(); return(scaled); } finally { if (bitmap != rep) { bitmap.Dispose(); } } }
public void Create(Stream stream) { Control = new NSImage(NSData.FromStream(stream)); rep = GetBestRepresentation(); bmprep = rep as NSBitmapImageRep; Control.Size = new CGSize(rep.PixelsWide, rep.PixelsHigh); alpha = rep.HasAlpha; }
public void Create(int width, int height, PixelFormat pixelFormat) { switch (pixelFormat) { case PixelFormat.Format32bppRgb: { alpha = false; const int numComponents = 4; const int bitsPerComponent = 8; const int bitsPerPixel = numComponents * bitsPerComponent; const int bytesPerPixel = bitsPerPixel / 8; int bytesPerRow = bytesPerPixel * width; rep = bmprep = new NSBitmapImageRep(IntPtr.Zero, width, height, bitsPerComponent, 3, false, false, NSColorSpace.DeviceRGB, bytesPerRow, bitsPerPixel); Control = new NSImage(); Control.AddRepresentation(rep); break; } case PixelFormat.Format24bppRgb: { alpha = false; const int numComponents = 3; const int bitsPerComponent = 8; const int bitsPerPixel = numComponents * bitsPerComponent; const int bytesPerPixel = bitsPerPixel / 8; int bytesPerRow = bytesPerPixel * width; rep = bmprep = new NSBitmapImageRep(IntPtr.Zero, width, height, bitsPerComponent, numComponents, false, false, NSColorSpace.DeviceRGB, bytesPerRow, bitsPerPixel); Control = new NSImage(); Control.AddRepresentation(rep); break; } case PixelFormat.Format32bppRgba: { alpha = true; const int numComponents = 4; const int bitsPerComponent = 8; const int bitsPerPixel = numComponents * bitsPerComponent; const int bytesPerPixel = bitsPerPixel / 8; int bytesPerRow = bytesPerPixel * width; rep = bmprep = new NSBitmapImageRep(IntPtr.Zero, width, height, bitsPerComponent, numComponents, true, false, NSColorSpace.DeviceRGB, bytesPerRow, bitsPerPixel); Control = new NSImage(); Control.AddRepresentation(rep); break; } /*case PixelFormat.Format16bppRgb555: * control = new Gdk.Pixbuf(Gdk.Colorspace.Rgb, false, 5, width, height); * break;*/ default: throw new ArgumentOutOfRangeException("pixelFormat", pixelFormat, string.Format(CultureInfo.CurrentCulture, "Not supported")); } }
public void Create(string fileName) { if (!File.Exists(fileName)) { throw new FileNotFoundException("Icon not found", fileName); } Control = new NSImage(fileName); rep = Control.BestRepresentationForDevice(null); bmprep = rep as NSBitmapImageRep; Control.Size = new CGSize(rep.PixelsWide, rep.PixelsHigh); }
protected override void Dispose(bool disposing) { if (disposing) { if (rep != null) { rep.SafeDispose(); rep = null; } } base.Dispose(disposing); }
public static NSImageRep Resize(this NSImageRep image, CGSize newsize, ImageInterpolation interpolation = ImageInterpolation.Default, CGSize?imageSize = null) { var newrep = new NSBitmapImageRep(IntPtr.Zero, (nint)newsize.Width, (nint)newsize.Height, 8, 4, true, false, NSColorSpace.DeviceRGB, 4 * (nint)newsize.Width, 32); newrep.Size = imageSize ?? newsize; var graphics = NSGraphicsContext.FromBitmap(newrep); NSGraphicsContext.GlobalSaveGraphicsState(); NSGraphicsContext.CurrentContext = graphics; graphics.GraphicsPort.InterpolationQuality = interpolation.ToCG(); image.DrawInRect(new CGRect(CGPoint.Empty, newrep.Size), CGRect.Empty, NSCompositingOperation.SourceOver, 1f, true, DrawHints); NSGraphicsContext.GlobalRestoreGraphicsState(); return(newrep); }
static NSImageRep GetIconRep(this Theme theme, string name, int size, bool selected, string suffix) { var composedName = theme.GetIconName(name, size, selected); var url = new NSUrl( $"Icons/{composedName}{suffix}.png", NSBundle.MainBundle.ResourceUrl); var rep = NSImageRep.ImageRepFromUrl(url); if (rep == null && suffix == "@2x") { rep = theme.GetIconRep(name, size * 2, selected, null); } return(rep); }
protected override Argb32DataWithSize GetImageData(string targetFile) { using (var imgRef = NSImageRep.ImageRepFromFile(targetFile)?.CGImage) { if (imgRef == null) { throw new InvalidOperationException(); } using (var dataProvider = imgRef.DataProvider) using (var data = dataProvider.CopyData()) { var source = data.Bytes; var bmp32Data = new byte[data.Length]; Marshal.Copy(source, bmp32Data, 0, bmp32Data.Length); return(new Argb32DataWithSize((int)imgRef.Width, (int)imgRef.Height, bmp32Data)); } } }
public static NSImageRep Resize(this NSImageRep image, CGSize newsize, ImageInterpolation interpolation = ImageInterpolation.Default, CGSize?imageSize = null) { var newrep = new NSBitmapImageRep(IntPtr.Zero, (nint)newsize.Width, (nint)newsize.Height, 8, 4, true, false, NSColorSpace.DeviceRGB, 4 * (nint)newsize.Width, 32); newrep.Size = imageSize ?? newsize; var graphics = NSGraphicsContext.FromBitmap(newrep); NSGraphicsContext.GlobalSaveGraphicsState(); NSGraphicsContext.CurrentContext = graphics; graphics.GraphicsPort.InterpolationQuality = interpolation.ToCG(); #if XAMMAC // Xamarin.Mac doesn't allow null for hints, remove this when it does. Messaging.bool_objc_msgSend_CGRect_CGRect_UIntPtr_nfloat_bool_IntPtr(image.Handle, selDrawInRect_FromRect_Operation_Fraction_RespectFlipped_Hints_Handle, new CGRect(CGPoint.Empty, newrep.Size), CGRect.Empty, (UIntPtr)(ulong)NSCompositingOperation.SourceOver, 1f, true, IntPtr.Zero); #else image.DrawInRect(new CGRect(CGPoint.Empty, newrep.Size), CGRect.Empty, NSCompositingOperation.SourceOver, 1f, true, null); #endif NSGraphicsContext.GlobalRestoreGraphicsState(); return(newrep); }
protected void EnsureRep() { if (rep == null) { rep = Control.BestRepresentationForDevice(null); } if (bmprep == null) { var lazyRep = rep as IconFrameHandler.LazyImageRep; if (lazyRep != null) { bmprep = lazyRep.Rep; } else { bmprep = rep as NSBitmapImageRep; if (bmprep == null) { rep = Control.BestRepresentationForDevice(null); bmprep = rep as NSBitmapImageRep; } } } }
public BitmapHandler(NSImage image) { Control = image; rep = Control.BestRepresentationForDevice(null); bmprep = rep as NSBitmapImageRep; }
protected void EnsureRep() { if (rep == null) { rep = GetBestRepresentation(); } // on Big Sur, rep is usually going to be a proxy, so let's find the concrete NSBitmapImageRep class the slow way.. if (bmprep != null) { return; } if (rep is IconFrameHandler.LazyImageRep lazyRep) { bmprep = lazyRep.Rep; } else { bmprep = rep as NSBitmapImageRep ?? GetBestRepresentation() as NSBitmapImageRep; } if (bmprep != null) { return; } // go through concrete representations as we might have a proxy (Big Sur) // this is fixed with MonoMac, but not Xamarin.Mac. var representations = Control.Representations(); for (int i = 0; i < representations.Length; i++) { NSImageRep rep = representations[i]; if (rep is NSBitmapImageRep brep) { bmprep = brep; return; } } // create a new bitmap rep and copy the contents var size = Size; int numComponents = rep.HasAlpha ? 4 : 3; int bitsPerComponent = 8; int bitsPerPixel = numComponents * bitsPerComponent; int bytesPerPixel = bitsPerPixel / 8; int bytesPerRow = bytesPerPixel * size.Width; bmprep = new NSBitmapImageRep(IntPtr.Zero, size.Width, size.Height, bitsPerComponent, numComponents, rep.HasAlpha, false, rep.ColorSpaceName, bytesPerRow, bitsPerPixel); var graphicsContext = NSGraphicsContext.FromBitmap(bmprep); NSGraphicsContext.GlobalSaveGraphicsState(); NSGraphicsContext.CurrentContext = graphicsContext; Control.Draw(CGPoint.Empty, new CGRect(CGPoint.Empty, size.ToNS()), NSCompositingOperation.Copy, 1); NSGraphicsContext.GlobalRestoreGraphicsState(); // remove all existing representations for (int i = 0; i < representations.Length; i++) { NSImageRep rep = representations[i]; Control.RemoveRepresentation(rep); } // add the new one back Control.AddRepresentation(bmprep); }
static LazyImageRep() { NSImageRep.RegisterImageRepClass(new Class(typeof(LazyImageRep))); }
public override Size GetSize(string file) { using (var rep = NSImageRep.ImageRepFromFile(file)) return(rep.Size.ToXwtSize()); }
public static Gdk.Pixbuf GetPixbufFromNSImageRep (NSImageRep rep, int width, int height) { var rect = new CGRect (0, 0, width, height); var bitmap = rep as NSBitmapImageRep; try { if (bitmap == null) { using (var cgi = rep.AsCGImage (ref rect, null, null)) { if (cgi == null) return null; bitmap = new NSBitmapImageRep (cgi); } } return GetPixbufFromNSBitmapImageRep (bitmap, width, height); } finally { if (bitmap != null) bitmap.Dispose (); } }
public void Create(int width, int height, PixelFormat pixelFormat) { switch (pixelFormat) { case PixelFormat.Format32bppRgb: { alpha = false; int numComponents = 4; int bitsPerComponent = 8; int bitsPerPixel = numComponents * bitsPerComponent; int bytesPerPixel = bitsPerPixel / 8; int bytesPerRow = bytesPerPixel * width; rep = bmprep = new NSBitmapImageRep(IntPtr.Zero, width, height, bitsPerComponent, 3, false, false, NSColorSpace.DeviceRGB, bytesPerRow, bitsPerPixel); Control = new NSImage(); Control.AddRepresentation(rep); //var provider = new CGDataProvider (data.Bytes, (int)data.Length); //var cgImage = new CGImage (width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, CGColorSpace.CreateDeviceRGB (), CGBitmapFlags.ByteOrder32Little | CGBitmapFlags.PremultipliedFirst, provider, null, true, CGColorRenderingIntent.Default); //Control = new NSImage (cgImage, new System.Drawing.SizeF (width, height)); break; } case PixelFormat.Format24bppRgb: { alpha = false; int numComponents = 3; int bitsPerComponent = 8; int bitsPerPixel = numComponents * bitsPerComponent; int bytesPerPixel = bitsPerPixel / 8; int bytesPerRow = bytesPerPixel * width; rep = bmprep = new NSBitmapImageRep(IntPtr.Zero, width, height, bitsPerComponent, numComponents, false, false, NSColorSpace.DeviceRGB, bytesPerRow, bitsPerPixel); Control = new NSImage(); Control.AddRepresentation(rep); //var provider = new CGDataProvider (data.ClassHandle); //var cgImage = new CGImage (width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, CGColorSpace.CreateDeviceRGB (), CGBitmapFlags.ByteOrder32Little | CGBitmapFlags.PremultipliedFirst, provider, null, true, CGColorRenderingIntent.Default); //Control = new NSImage (cgImage, new System.Drawing.SizeF (width, height)); break; } case PixelFormat.Format32bppRgba: { alpha = true; int numComponents = 4; int bitsPerComponent = 8; int bitsPerPixel = numComponents * bitsPerComponent; int bytesPerPixel = bitsPerPixel / 8; int bytesPerRow = bytesPerPixel * width; rep = bmprep = new NSBitmapImageRep(IntPtr.Zero, width, height, bitsPerComponent, numComponents, true, false, NSColorSpace.DeviceRGB, bytesPerRow, bitsPerPixel); Control = new NSImage(); Control.AddRepresentation(rep); //var provider = new CGDataProvider (data.Bytes, (int)data.Length); //var cgImage = new CGImage (width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, CGColorSpace.CreateDeviceRGB (), CGBitmapFlags.ByteOrder32Little | CGBitmapFlags.PremultipliedFirst, provider, null, true, CGColorRenderingIntent.Default); //Control = new NSImage (cgImage, new System.Drawing.SizeF (width, height)); break; } /*case PixelFormat.Format16bppRgb555: * control = new Gdk.Pixbuf(Gdk.Colorspace.Rgb, false, 5, width, height); * break;*/ default: throw new ArgumentOutOfRangeException("pixelFormat", pixelFormat, "Not supported"); } }