public void Import(LayerManager layers, string fileName) { Pixbuf bg = new Pixbuf (fileName); layers.Clear (); PintaCore.History.Clear (); layers.DestroySelectionLayer (); PintaCore.Workspace.ImageSize = new Size (bg.Width, bg.Height); PintaCore.Workspace.CanvasSize = new Gdk.Size (bg.Width, bg.Height); layers.ResetSelectionPath (); Layer layer = layers.AddNewLayer (Path.GetFileName (fileName)); using (Cairo.Context g = new Cairo.Context (layer.Surface)) { CairoHelper.SetSourcePixbuf (g, bg, 0, 0); g.Paint (); } bg.Dispose (); }
public void Import(LayerManager layers, string fileName) { ZipFile file = new ZipFile (fileName); XmlDocument stackXml = new XmlDocument (); stackXml.Load (file.GetInputStream (file.GetEntry ("stack.xml"))); XmlElement imageElement = stackXml.DocumentElement; int width = int.Parse (imageElement.GetAttribute ("w")); int height = int.Parse (imageElement.GetAttribute ("h")); XmlElement stackElement = (XmlElement) stackXml.GetElementsByTagName ("stack")[0]; XmlNodeList layerElements = stackElement.GetElementsByTagName ("layer"); if (layerElements.Count == 0) throw new XmlException ("No layers found in OpenRaster file"); layers.Clear (); PintaCore.History.Clear (); layers.DestroySelectionLayer (); PintaCore.Workspace.ImageSize = new Size (width, height); PintaCore.Workspace.CanvasSize = new Gdk.Size (width, height); for (int i = 0; i < layerElements.Count; i++) { XmlElement layerElement = (XmlElement) layerElements[i]; int x = int.Parse (GetAttribute (layerElement, "x", "0")); int y = int.Parse (GetAttribute (layerElement, "y", "0")); string name = GetAttribute (layerElement, "name", string.Format ("Layer {0}", i)); try { // Write the file to a temporary file first // Fixes a bug when running on .Net ZipEntry zf = file.GetEntry (layerElement.GetAttribute ("src")); Stream s = file.GetInputStream (zf); string tmp_file = System.IO.Path.GetTempFileName (); using (Stream stream_out = File.Open (tmp_file, FileMode.OpenOrCreate)) { byte[] buffer = new byte[2048]; while (true) { int len = s.Read (buffer, 0, buffer.Length); if (len > 0) stream_out.Write (buffer, 0, len); else break; } } Layer layer = layers.CreateLayer (name, width, height); layers.Insert (layer, 0); layer.Opacity = double.Parse (GetAttribute (layerElement, "opacity", "1"), GetFormat ()); using (Pixbuf pb = new Pixbuf (tmp_file)) { using (Context g = new Context (layer.Surface)) { CairoHelper.SetSourcePixbuf (g, pb, x, y); g.Paint (); } } try { File.Delete (tmp_file); } catch { } } catch { MessageDialog md = new MessageDialog (PintaCore.Chrome.MainWindow, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, "Could not import layer \"{0}\" from {0}", name, file); md.Title = "Error"; md.Run (); md.Destroy (); } } file.Close (); }