internal MyScreenshot(string path, Vector2 sizeMult, bool ignoreSprites, bool showNotification) { SavePath = path ?? GetDefaultScreenshotFilenameWithExtension(); Format = GetFormat(Path.GetExtension(SavePath).ToLower()); SizeMult = sizeMult; IgnoreSprites = ignoreSprites; ShowNotification = showNotification; }
internal MyScreenshot(string path, Vector2 sizeMult, bool ignoreSprites, bool showNotification) { SavePath = path ?? GetDefaultScreenshotFilenameWithExtension(); Format = GetFormat(Path.GetExtension(SavePath).ToLower()); SizeMult = sizeMult; IgnoreSprites = ignoreSprites; ShowNotification = showNotification; }
internal static byte[] ToData(Resource res, byte[] screenData, ImageFileFormat fmt) { try { using (var stream = screenData == null ? new MemoryStream() : new MemoryStream(screenData, true)) { Save(res, stream, fmt); return(stream.GetBuffer()); } } catch (Exception e) { MyRender11.Log.WriteLine("MyTextureData.ToData()"); MyRender11.Log.IncreaseIndent(); MyRender11.Log.WriteLine(string.Format("Failed to extract data: {0}", e)); MyRender11.Log.DecreaseIndent(); return(null); } }
internal static byte[] ToData(IResource res, byte[] screenData, ImageFileFormat fmt) { try { using (var stream = screenData == null ? new MemoryStream() : new MemoryStream(screenData, true)) { Save(res, stream, fmt); return stream.GetBuffer(); } } catch (Exception e) { MyRender11.Log.WriteLine("MyTextureData.ToData()"); MyRender11.Log.IncreaseIndent(); MyRender11.Log.WriteLine(string.Format("Failed to extract data: {0}", e)); MyRender11.Log.DecreaseIndent(); return null; } }
internal static bool ToFile(Resource res, string path, ImageFileFormat fmt) { try { using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write)) { Save(res, stream, fmt); } return(true); } catch (SharpDX.SharpDXException e) { MyRender11.Log.WriteLine("SaveResourceToFile()"); MyRender11.Log.IncreaseIndent(); MyRender11.Log.WriteLine(string.Format("Failed to save screenshot {0}: {1}", path, e)); MyRender11.Log.DecreaseIndent(); return(false); } }
internal static bool ToFile(IResource res, string path, ImageFileFormat fmt) { try { using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write)) { Save(res, stream, fmt); } return true; } catch (SharpDX.SharpDXException e) { MyRender11.Log.WriteLine("SaveResourceToFile()"); MyRender11.Log.IncreaseIndent(); MyRender11.Log.WriteLine(string.Format("Failed to save screenshot {0}: {1}", path, e)); MyRender11.Log.DecreaseIndent(); return false; } }
internal static bool ToFile(IResource res, string path, ImageFileFormat fmt) { try { System.IO.Directory.CreateDirectory(Path.GetDirectoryName(path)); // if the directory is not created, the "new FileStream" will fail using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write)) { Save(res, stream, fmt); } return(true); } catch (Exception e) { MyRender11.Log.WriteLine("SaveResourceToFile()"); MyRender11.Log.IncreaseIndent(); MyRender11.Log.WriteLine(string.Format("Failed to save screenshot {0}: {1}", path, e)); MyRender11.Log.DecreaseIndent(); return(false); } }
private unsafe static byte[] GetScreenData(Resource res, byte[] screenData, ImageFileFormat fmt) { return(MyTextureData.ToData(res, screenData, fmt)); }
private static void Save(Resource res, Stream stream, ImageFileFormat fmt) { var texture = res as Texture2D; var textureCopy = new Texture2D(MyRender11.Device, new Texture2DDescription { Width = (int)texture.Description.Width, Height = (int)texture.Description.Height, MipLevels = 1, ArraySize = 1, Format = texture.Description.Format, Usage = ResourceUsage.Staging, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), BindFlags = BindFlags.None, CpuAccessFlags = CpuAccessFlags.Read, OptionFlags = ResourceOptionFlags.None }); RC.CopyResource(res, textureCopy); DataStream dataStream; var dataBox = RC.MapSubresource( textureCopy, 0, 0, MapMode.Read, MapFlags.None, out dataStream); var dataRectangle = new DataRectangle { DataPointer = dataStream.DataPointer, Pitch = dataBox.RowPitch }; var bitmap = new Bitmap( MyRender11.WIC, textureCopy.Description.Width, textureCopy.Description.Height, PixelFormatFromFormat(textureCopy.Description.Format), // TODO: should use some conversion from textureCopy.Description.Format dataRectangle); using (var wicStream = new WICStream(MyRender11.WIC, stream)) { BitmapEncoder bitmapEncoder; switch (fmt) { case ImageFileFormat.Png: bitmapEncoder = new PngBitmapEncoder(MyRender11.WIC, wicStream); break; case ImageFileFormat.Jpg: bitmapEncoder = new JpegBitmapEncoder(MyRender11.WIC, wicStream); break; case ImageFileFormat.Bmp: bitmapEncoder = new BmpBitmapEncoder(MyRender11.WIC, wicStream); break; default: MyRenderProxy.Assert(false, "Unsupported file format."); bitmapEncoder = null; break; } if (bitmapEncoder != null) { using (var bitmapFrameEncode = new BitmapFrameEncode(bitmapEncoder)) { bitmapFrameEncode.Initialize(); bitmapFrameEncode.SetSize(bitmap.Size.Width, bitmap.Size.Height); var pixelFormat = PixelFormat.FormatDontCare; bitmapFrameEncode.SetPixelFormat(ref pixelFormat); bitmapFrameEncode.WriteSource(bitmap); bitmapFrameEncode.Commit(); bitmapEncoder.Commit(); } bitmapEncoder.Dispose(); } } RC.UnmapSubresource(textureCopy, 0); textureCopy.Dispose(); bitmap.Dispose(); }
private static void Save(IResource res, Stream stream, ImageFileFormat fmt) { var texture = res.Resource as Texture2D; var textureCopy = new Texture2D(MyRender11.Device, new Texture2DDescription { Width = (int)texture.Description.Width, Height = (int)texture.Description.Height, MipLevels = 1, ArraySize = 1, Format = texture.Description.Format, Usage = ResourceUsage.Staging, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), BindFlags = BindFlags.None, CpuAccessFlags = CpuAccessFlags.Read, OptionFlags = ResourceOptionFlags.None }); RC.CopyResource(res, textureCopy); DataStream dataStream; var dataBox = RC.MapSubresource( textureCopy, 0, 0, MapMode.Read, MapFlags.None, out dataStream); var dataRectangle = new DataRectangle { DataPointer = dataStream.DataPointer, Pitch = dataBox.RowPitch }; var bitmap = new Bitmap( MyRender11.WIC, textureCopy.Description.Width, textureCopy.Description.Height, PixelFormatFromFormat(textureCopy.Description.Format), // TODO: should use some conversion from textureCopy.Description.Format dataRectangle); using (var wicStream = new WICStream(MyRender11.WIC, stream)) { BitmapEncoder bitmapEncoder; switch (fmt) { case ImageFileFormat.Png: bitmapEncoder = new PngBitmapEncoder(MyRender11.WIC, wicStream); break; case ImageFileFormat.Jpg: bitmapEncoder = new JpegBitmapEncoder(MyRender11.WIC, wicStream); break; case ImageFileFormat.Bmp: bitmapEncoder = new BmpBitmapEncoder(MyRender11.WIC, wicStream); break; default: MyRenderProxy.Assert(false, "Unsupported file format."); bitmapEncoder = null; break; } if (bitmapEncoder != null) { using (var bitmapFrameEncode = new BitmapFrameEncode(bitmapEncoder)) { bitmapFrameEncode.Initialize(); bitmapFrameEncode.SetSize(bitmap.Size.Width, bitmap.Size.Height); var pixelFormat = PixelFormat.FormatDontCare; bitmapFrameEncode.SetPixelFormat(ref pixelFormat); bitmapFrameEncode.WriteSource(bitmap); bitmapFrameEncode.Commit(); bitmapEncoder.Commit(); } bitmapEncoder.Dispose(); } } RC.UnmapSubresource(textureCopy, 0); textureCopy.Dispose(); bitmap.Dispose(); }
private unsafe static byte[] GetScreenData(Resource res, byte[] screenData, ImageFileFormat fmt) { return MyTextureData.ToData(res, screenData, fmt); }