public void FromUrl_BadITU() { using (NSUrl url = NSUrl.FromString("file://local")) { #if XAMCORE_2_0 // FromUrl => Create Assert.Null(CGImageDestination.Create(url, BadUti, 1), "FromUrl-1"); #else Assert.Null(CGImageDestination.FromUrl(url, BadUti, 1), "FromUrl-1"); Assert.Null(CGImageDestination.FromUrl(url, BadUti, 1, new CGImageDestinationOptions()), "FromUrl-2"); #endif } }
public void ExportEveryGlyphsAsPNGTo(string path) { float scale = Window.BackingScaleFactor; var glyphInfos = _tableViewSource.GlyphInfos; //actvw.StartAnimating (); Task.Factory.StartNew(() => { Func <string, string> makeName = (rawName) => { return(String.Format("{0}_{1}{2}.png", rawName, (int)this.unitFontSize , ((scale > 2f) ? "@4x" : (scale > 1f) ? "@2x" : ""))); }; //string documentDirPath = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments); string documentDirPath = path; NSDictionary prop = new NSDictionary(); // int nSucceeded = 0; int nFailed = 0; glyphInfos.ForEach((gi) => { var url = new NSUrl(System.IO.Path.Combine(documentDirPath, makeName(gi.RawName)), false); try { CGImageDestination dest = CGImageDestination.FromUrl(url, "public.png", 1); dest.AddImage(gi.GlyphImage, prop); dest.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } if (System.IO.File.Exists(url.Path)) { //Console.WriteLine ("Succeeded to write {0}", url); ++nSucceeded; } else { Console.WriteLine("Failed to write {0}", url); ++nFailed; } BeginInvokeOnMainThread(() => { //ShowProgressionLabel (String.Format ("{0} of {1} processed", (nSucceeded + nFailed), glyphInfos.Count)); }); }); InvokeOnMainThread(() => { //actvw.StopAnimating (); //DismissProgressionLabel (); var alert = new NSAlert(); alert.MessageText = String.Format("{0} succeeded, {1} failed", nSucceeded, nFailed); alert.RunModal(); }); }); }
private void SaveEveryImageToPNG() { float scale = UIScreen.MainScreen.Scale; var glyphInfos = _tableViewSource.GlyphInfos; actvw.StartAnimating(); Task.Factory.StartNew(() => { Func <string, string> makeName = (rawName) => { return(String.Format("{0}_{1}{2}.png", rawName, (int)this.unitFontSize , ((scale > 2f) ? "@4x" : (scale > 1f) ? "@2x" : ""))); }; string documentDirPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); NSDictionary prop = new NSDictionary(); // int nSucceeded = 0; int nFailed = 0; glyphInfos.ForEach((gi) => { var url = new NSUrl(System.IO.Path.Combine(documentDirPath, makeName(gi.RawName)), false); try { CGImageDestination dest = CGImageDestination.FromUrl(url, "public.png", 1); dest.AddImage(gi.GlyphImage, prop); dest.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } if (File.Exists(url.Path)) { //Console.WriteLine ("Succeeded to write {0}", url); ++nSucceeded; } else { Console.WriteLine("Failed to write {0}", url); ++nFailed; } BeginInvokeOnMainThread(() => { ShowProgressionLabel(String.Format("{0} of {1} processed", (nSucceeded + nFailed), glyphInfos.Count)); }); }); InvokeOnMainThread(() => { actvw.StopAnimating(); DismissProgressionLabel(); var alert = new UIAlertView("Result of save" , String.Format("{0} succeeded, {1} failed", nSucceeded, nFailed) , null, "Close", null); alert.Show(); }); }); }
public void Save(string path) { var extension = Path.GetExtension(path).ToLowerInvariant(); string uttype; switch (extension) { case ".png": uttype = "public.png"; break; case ".jpg": case ".jpeg": uttype = "public.jpeg"; break; default: throw new NotSupportedException(string.Format( "FramePixelData.Save cannot save images of type: {0}", extension)); } //var documentsDir = Environment.GetFolderPath (Environment.SpecialFolder.Personal); var handle = GCHandle.Alloc(_data, GCHandleType.Pinned); try { var sizeOfColor = Marshal.SizeOf(typeof(Color)); using (var dataProvider = new CGDataProvider( handle.AddrOfPinnedObject(), _data.Length * sizeOfColor, false)) using (var colorSpace = CGColorSpace.CreateDeviceRGB()) using (var image = new CGImage( Width, Height, 8, 32, Width * sizeOfColor, colorSpace, CGBitmapFlags.ByteOrder32Little | CGBitmapFlags.First, dataProvider, null, false, CGColorRenderingIntent.Default)) { //var fullPath = Path.Combine (documentsDir, path); Directory.CreateDirectory(Path.GetDirectoryName(path)); var url = NSUrl.FromFilename(Path.GetFullPath(path)); var destination = CGImageDestination.FromUrl(url, uttype, 1); destination.AddImage(image, null); if (!destination.Close()) { throw new Exception(string.Format( "Failed to write the image to '{0}'", path)); } } } finally { handle.Free(); } }
public new void Save(string path, ImageFormat format) { if (path == null) { throw new ArgumentNullException("path"); } // Obtain a URL file path to be passed NSUrl url = NSUrl.FromFilename(path); // Create an image destination that saves into the path that is passed in #if !XAMARINMAC using (var dest = CGImageDestination.FromUrl(url, GetTypeIdentifier(format), frameCount, null)) #else using (var dest = CGImageDestination.Create(url, GetTypeIdentifier(format), frameCount)) #endif Save(dest); }
// static CCVerticalTextAlignment vertical; // static CCTextAlignment horizontal; // Used for debuggin purposes internal static void SaveToFile(string fileName, CGBitmapContext bitmap) { if (bitmap == null) { throw new ObjectDisposedException("cgimage"); } // With MonoTouch we can use UTType from CoreMobileServices but since // MonoMac does not have that yet (or at least can not find it) I will // use the string version of those for now. I did not want to add another // #if #else in here. // for now we will just default this to png var typeIdentifier = "public.png"; // * NOTE * we only support one image for right now. //NSMutableData imgData = new NSMutableData(); NSUrl url = NSUrl.FromFilename(fileName); // Create an image destination that saves into the imgData #if IOS CGImageDestination dest = CGImageDestination.Create(url, typeIdentifier, 1); #else CGImageDestination dest = CGImageDestination.FromUrl(url, typeIdentifier, 1); #endif // Add an image to the destination dest.AddImage(bitmap.GetImage(), (NSDictionary)null); // Finish the export //bool success = dest.Close (); // if (success == false) // Console.WriteLine("did not work"); // else // Console.WriteLine("did work: " + path); dest.Dispose(); dest = null; }
public void Save(string path, ImageFormat format) { if (path == null) { throw new ArgumentNullException("path"); } if (NativeCGImage == null) { throw new ObjectDisposedException("cgimage"); } // With MonoTouch we can use UTType from CoreMobileServices but since // MonoMac does not have that yet (or at least can not find it) I will // use the string version of those for now. I did not want to add another // #if #else in here. // for now we will just default this to png var typeIdentifier = "public.png"; // Get the correct type identifier if (format == ImageFormat.Bmp) { typeIdentifier = "com.microsoft.bmp"; } // else if (format == ImageFormat.Emf) // typeIdentifier = "image/emf"; // else if (format == ImageFormat.Exif) // typeIdentifier = "image/exif"; else if (format == ImageFormat.Gif) { typeIdentifier = "com.compuserve.gif"; } else if (format == ImageFormat.Icon) { typeIdentifier = "com.microsoft.ico"; } else if (format == ImageFormat.Jpeg) { typeIdentifier = "public.jpeg"; } else if (format == ImageFormat.Png) { typeIdentifier = "public.png"; } else if (format == ImageFormat.Tiff) { typeIdentifier = "public.tiff"; } else if (format == ImageFormat.Wmf) { typeIdentifier = "com.adobe.pdf"; } // Not sure what this is yet else if (format == ImageFormat.MemoryBmp) { throw new NotImplementedException("ImageFormat.MemoryBmp not supported"); } // Obtain a URL file path to be passed NSUrl url = NSUrl.FromFilename(path); // * NOTE * we only support one image for right now. // Create an image destination that saves into the path that is passed in CGImageDestination dest = CGImageDestination.FromUrl(url, typeIdentifier, imageCount, null); // Add an image to the destination dest.AddImage(NativeCGImage, null); // Finish the export bool success = dest.Close(); // if (success == false) // Console.WriteLine("did not work"); // else // Console.WriteLine("did work: " + path); dest.Dispose(); dest = null; }