public override void SaveFileDialog(byte[] data, FolderLocations folderLocation, string[] fileTypes, StreamSavedCallbackMethod streamSavedCallback) { if (streamSavedCallback == null) { return; } // open native dlg var file = new OPENFILENAME(); file.lStructSize = (uint)Marshal.SizeOf(typeof(OPENFILENAME)); file.hwndOwner = IntPtr.Zero; file.lpstrDefExt = IntPtr.Zero; file.lpstrFile = Marshal.AllocHGlobal((int)MAX_PATH); unsafe { ((byte *)file.lpstrFile.ToPointer())[0] = 0; } file.nMaxFile = MAX_PATH; file.lpstrFilter = generateFilterValue(fileTypes); file.nFilterIndex = 0; file.lpstrInitialDir = Marshal.StringToHGlobalUni(Application.dataPath); file.lpstrTitle = Marshal.StringToHGlobalUni("Save file"); file.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR; GetSaveFileName(ref file); // get native dlg result string filename = null; if (file.lpstrFile != IntPtr.Zero) { filename = Marshal.PtrToStringUni(file.lpstrFile); Debug.Log("Saving file: " + filename); } Marshal.FreeHGlobal(file.lpstrFile); Marshal.FreeHGlobal(file.lpstrInitialDir); Marshal.FreeHGlobal(file.lpstrTitle); Marshal.FreeHGlobal(file.lpstrFilter); // save file if (!string.IsNullOrEmpty(filename)) { using (var stream = new FileStream(filename, FileMode.Create, FileAccess.Write)) { stream.Write(data, 0, data.Length); } if (streamSavedCallback != null) { streamSavedCallback(true); } } else { if (streamSavedCallback != null) { streamSavedCallback(false); } } }
private extern static void GetSaveFileName(ref OPENFILENAME lpofn);
public override void LoadFileDialog(FolderLocations folderLocation, int maxWidth, int maxHeight, int x, int y, int width, int height, string[] fileTypes, StreamLoadedCallbackMethod streamLoadedCallback) { if (streamLoadedCallback == null) { return; } // open native dlg var file = new OPENFILENAME(); file.lStructSize = (uint)Marshal.SizeOf(typeof(OPENFILENAME)); file.hwndOwner = IntPtr.Zero; file.lpstrDefExt = IntPtr.Zero; file.lpstrFile = Marshal.AllocHGlobal((int)MAX_PATH); unsafe { ((byte *)file.lpstrFile.ToPointer())[0] = 0; } file.nMaxFile = MAX_PATH; file.lpstrFilter = generateFilterValue(fileTypes); file.nFilterIndex = 0; file.lpstrInitialDir = Marshal.StringToHGlobalUni(Application.dataPath); file.lpstrTitle = Marshal.StringToHGlobalUni("Load file"); file.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR; GetOpenFileName(ref file); // get native dlg result string filename = null; if (file.lpstrFile != IntPtr.Zero) { filename = Marshal.PtrToStringUni(file.lpstrFile); Debug.Log("Loading file: " + filename); } Marshal.FreeHGlobal(file.lpstrFile); Marshal.FreeHGlobal(file.lpstrInitialDir); Marshal.FreeHGlobal(file.lpstrTitle); Marshal.FreeHGlobal(file.lpstrFilter); // open file if (!string.IsNullOrEmpty(filename)) { if (maxWidth == 0 || maxHeight == 0 || folderLocation != FolderLocations.Pictures) { streamLoadedCallback(new FileStream(filename, FileMode.Open, FileAccess.Read), true); } else { var newStream = new MemoryStream(); try { using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read)) { ImageTools.IO.IImageDecoder decoder = null; switch (Path.GetExtension(filename).ToLower()) { case ".jpg": decoder = new ImageTools.IO.Jpeg.JpegDecoder(); break; case ".jpeg": decoder = new ImageTools.IO.Jpeg.JpegDecoder(); break; case ".png": decoder = new ImageTools.IO.Png.PngDecoder(); break; default: Debug.LogError("Unsuported file ext type: " + Path.GetExtension(filename)); streamLoadedCallback(null, false); return; } var image = new ExtendedImage(); decoder.Decode(image, stream); var newSize = Reign.MathUtilities.FitInViewIfLarger(image.PixelWidth, image.PixelHeight, maxWidth, maxHeight); var newImage = ExtendedImage.Resize(image, (int)newSize.x, (int)newSize.y, new NearestNeighborResizer()); var encoder = new PngEncoder(); encoder.Encode(newImage, newStream); newStream.Position = 0; } } catch (Exception e) { newStream.Dispose(); newStream = null; Debug.LogError(e.Message); } finally { streamLoadedCallback(newStream, true); } } } else { streamLoadedCallback(null, false); } }
public override void SaveFileDialog(byte[] data, FolderLocations folderLocation, string[] fileTypes, StreamSavedCallbackMethod streamSavedCallback) { if (streamSavedCallback == null) return; // open native dlg var file = new OPENFILENAME(); file.lStructSize = (uint)Marshal.SizeOf(typeof(OPENFILENAME)); file.hwndOwner = IntPtr.Zero; file.lpstrDefExt = IntPtr.Zero; file.lpstrFile = Marshal.AllocHGlobal((int)MAX_PATH); unsafe {((byte*)file.lpstrFile.ToPointer())[0] = 0;} file.nMaxFile = MAX_PATH; file.lpstrFilter = generateFilterValue(fileTypes); file.nFilterIndex = 0; file.lpstrInitialDir = Marshal.StringToHGlobalUni(Application.dataPath); file.lpstrTitle = Marshal.StringToHGlobalUni("Save file"); file.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR; GetSaveFileName(ref file); // get native dlg result string filename = null; if (file.lpstrFile != IntPtr.Zero) { filename = Marshal.PtrToStringUni(file.lpstrFile); Debug.Log("Saving file: " + filename); } Marshal.FreeHGlobal(file.lpstrFile); Marshal.FreeHGlobal(file.lpstrInitialDir); Marshal.FreeHGlobal(file.lpstrTitle); Marshal.FreeHGlobal(file.lpstrFilter); // save file if (!string.IsNullOrEmpty(filename)) { using (var stream = new FileStream(filename, FileMode.Create, FileAccess.Write)) { stream.Write(data, 0, data.Length); } if (streamSavedCallback != null) streamSavedCallback(true); } else { if (streamSavedCallback != null) streamSavedCallback(false); } }
public override void LoadFileDialog(FolderLocations folderLocation, int maxWidth, int maxHeight, int x, int y, int width, int height, string[] fileTypes, StreamLoadedCallbackMethod streamLoadedCallback) { if (streamLoadedCallback == null) return; // open native dlg var file = new OPENFILENAME(); file.lStructSize = (uint)Marshal.SizeOf(typeof(OPENFILENAME)); file.hwndOwner = IntPtr.Zero; file.lpstrDefExt = IntPtr.Zero; file.lpstrFile = Marshal.AllocHGlobal((int)MAX_PATH); unsafe {((byte*)file.lpstrFile.ToPointer())[0] = 0;} file.nMaxFile = MAX_PATH; file.lpstrFilter = generateFilterValue(fileTypes); file.nFilterIndex = 0; file.lpstrInitialDir = Marshal.StringToHGlobalUni(Application.dataPath); file.lpstrTitle = Marshal.StringToHGlobalUni("Load file"); file.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR; GetOpenFileName(ref file); // get native dlg result string filename = null; if (file.lpstrFile != IntPtr.Zero) { filename = Marshal.PtrToStringUni(file.lpstrFile); Debug.Log("Loading file: " + filename); } Marshal.FreeHGlobal(file.lpstrFile); Marshal.FreeHGlobal(file.lpstrInitialDir); Marshal.FreeHGlobal(file.lpstrTitle); Marshal.FreeHGlobal(file.lpstrFilter); // open file if (!string.IsNullOrEmpty(filename)) { if (maxWidth == 0 || maxHeight == 0 || folderLocation != FolderLocations.Pictures) { streamLoadedCallback(new FileStream(filename, FileMode.Open, FileAccess.Read), true); } else { var newStream = new MemoryStream(); try { using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read)) { ImageTools.IO.IImageDecoder decoder = null; switch (Path.GetExtension(filename).ToLower()) { case ".jpg": decoder = new ImageTools.IO.Jpeg.JpegDecoder(); break; case ".jpeg": decoder = new ImageTools.IO.Jpeg.JpegDecoder(); break; case ".png": decoder = new ImageTools.IO.Png.PngDecoder(); break; default: Debug.LogError("Unsuported file ext type: " + Path.GetExtension(filename)); streamLoadedCallback(null, false); return; } var image = new ExtendedImage(); decoder.Decode(image, stream); var newSize = Reign.MathUtilities.FitInViewIfLarger(image.PixelWidth, image.PixelHeight, maxWidth, maxHeight); var newImage = ExtendedImage.Resize(image, (int)newSize.x, (int)newSize.y, new NearestNeighborResizer()); var encoder = new PngEncoder(); encoder.Encode(newImage, newStream); newStream.Position = 0; } } catch (Exception e) { newStream.Dispose(); newStream = null; Debug.LogError(e.Message); } finally { streamLoadedCallback(newStream, true); } } } else { streamLoadedCallback(null, false); } }