diff --git a/src/BannedSymbols.BannedApiAnalyzers.txt b/src/BannedSymbols.BannedApiAnalyzers.txt index 4c986bf2004..0b5aea6a9e8 100644 --- a/src/BannedSymbols.BannedApiAnalyzers.txt +++ b/src/BannedSymbols.BannedApiAnalyzers.txt @@ -22,6 +22,8 @@ M:System.Diagnostics.Debug.Assert(System.Boolean);include a unique message M:System.Diagnostics.Debug.WriteLine(System.String);use Util.DebugWriteLine (first-party) because apparently the BCL's version doesn't work properly in some circumstances? M:System.Diagnostics.Trace.Assert(System.Boolean);include a unique message M:System.Diagnostics.Trace.Assert(System.Boolean,System.String);just do the check and throw a specific exception +M:System.Drawing.Bitmap.#ctor(System.Int32,System.Int32);use BitmapBuffer.CreateBitmapObject +M:System.Drawing.Bitmap.#ctor(System.Int32,System.Int32,System.Drawing.Imaging.PixelFormat);use BitmapBuffer.CreateBitmapObject M:System.Random.#ctor;this is a source of nondeterminism M:System.String.GetHashCode;use StableStringHash extension P:System.StringComparer.InvariantCultureIgnoreCase;use OrdinalIgnoreCase diff --git a/src/BizHawk.Bizware.Graphics/BitmapBuffer.cs b/src/BizHawk.Bizware.Graphics/BitmapBuffer.cs index 510e23f39b4..53adf74175e 100644 --- a/src/BizHawk.Bizware.Graphics/BitmapBuffer.cs +++ b/src/BizHawk.Bizware.Graphics/BitmapBuffer.cs @@ -24,6 +24,13 @@ namespace BizHawk.Bizware.Graphics /// public unsafe class BitmapBuffer : IDisposable { + public static Bitmap CreateBitmapObject(Size size, PixelFormat format = PixelFormat.Format32bppArgb) + => size.Width is >= 1 and <= 0xFFFF || size.Height is >= 1 and <= 0xFFFF +#pragma warning disable RS0030 // this is the sanctioned call-site + ? new Bitmap(width: size.Width, height: size.Height, format) +#pragma warning restore RS0030 + : throw new ArgumentOutOfRangeException(paramName: nameof(size), size, message: "width and height must each be in the range 1..<65536"); + public int Width, Height; public int[] Pixels; @@ -568,7 +575,7 @@ public Bitmap ToSysdrawingBitmap() } var pf = HasAlpha ? PixelFormat.Format32bppArgb : PixelFormat.Format24bppRgb; - var bmp = new Bitmap(Width, Height, pf); + var bmp = CreateBitmapObject(Size, pf); ToSysdrawingBitmap(bmp); return bmp; } diff --git a/src/BizHawk.Bizware.Graphics/GDIPlus/GDIPlusTexture2D.cs b/src/BizHawk.Bizware.Graphics/GDIPlus/GDIPlusTexture2D.cs index e2f857860a8..d2190bbc2ee 100644 --- a/src/BizHawk.Bizware.Graphics/GDIPlus/GDIPlusTexture2D.cs +++ b/src/BizHawk.Bizware.Graphics/GDIPlus/GDIPlusTexture2D.cs @@ -1,5 +1,4 @@ using System.Drawing; -using System.Drawing.Imaging; namespace BizHawk.Bizware.Graphics { @@ -16,7 +15,7 @@ public GDIPlusTexture2D(int width, int height) { Width = width; Height = height; - SDBitmap = new(width, height, PixelFormat.Format32bppArgb); + SDBitmap = BitmapBuffer.CreateBitmapObject(new(width: width, height: height)); } public virtual void Dispose() diff --git a/src/BizHawk.Bizware.Graphics/Renderers/ImGui2DRenderer.cs b/src/BizHawk.Bizware.Graphics/Renderers/ImGui2DRenderer.cs index 43e369469e6..abef2d9778f 100644 --- a/src/BizHawk.Bizware.Graphics/Renderers/ImGui2DRenderer.cs +++ b/src/BizHawk.Bizware.Graphics/Renderers/ImGui2DRenderer.cs @@ -318,7 +318,7 @@ public ITexture2D Render(int width, int height) _stringTexture?.Dispose(); _stringGraphics?.Dispose(); _stringOutput?.Dispose(); - _stringOutput = new(width, height, PixelFormat.Format32bppArgb); + _stringOutput = BitmapBuffer.CreateBitmapObject(new(width: width, height: height)); _stringGraphics = SDGraphics.FromImage(_stringOutput); _stringTexture = _igl.CreateTexture(width, height); } diff --git a/src/BizHawk.Client.Common/Api/Classes/GuiApi.cs b/src/BizHawk.Client.Common/Api/Classes/GuiApi.cs index e9741f65da5..79721990f6f 100644 --- a/src/BizHawk.Client.Common/Api/Classes/GuiApi.cs +++ b/src/BizHawk.Client.Common/Api/Classes/GuiApi.cs @@ -32,7 +32,7 @@ public sealed class GuiApi : IGuiApi private readonly Dictionary _imageCache = new(); - private readonly Bitmap _nullGraphicsBitmap = new(1, 1); + private readonly Bitmap _nullGraphicsBitmap = BitmapBuffer.CreateBitmapObject(new(1, 1)); private CompositingMode _compositingMode = CompositingMode.SourceOver; diff --git a/src/BizHawk.Client.EmuHawk/AVOut/GifWriter.cs b/src/BizHawk.Client.EmuHawk/AVOut/GifWriter.cs index 3fff064366e..ecfb4339a64 100644 --- a/src/BizHawk.Client.EmuHawk/AVOut/GifWriter.cs +++ b/src/BizHawk.Client.EmuHawk/AVOut/GifWriter.cs @@ -1,6 +1,7 @@ using System.IO; using System.Drawing; +using BizHawk.Bizware.Graphics; using BizHawk.Client.Common; using BizHawk.Emulation.Common; @@ -167,7 +168,7 @@ public void AddFrame(IVideoProvider source) return; // skip this frame } - using var bmp = new Bitmap(source.BufferWidth, source.BufferHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb); + using var bmp = BitmapBuffer.CreateBitmapObject(new(width: source.BufferWidth, height: source.BufferHeight)); var data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); System.Runtime.InteropServices.Marshal.Copy(source.GetVideoBuffer(), 0, data.Scan0, bmp.Width * bmp.Height); bmp.UnlockBits(data); diff --git a/src/BizHawk.Client.EmuHawk/CustomControls/ViewportPanel.cs b/src/BizHawk.Client.EmuHawk/CustomControls/ViewportPanel.cs index b30c1172952..3037e3be95c 100644 --- a/src/BizHawk.Client.EmuHawk/CustomControls/ViewportPanel.cs +++ b/src/BizHawk.Client.EmuHawk/CustomControls/ViewportPanel.cs @@ -4,6 +4,8 @@ using System.Drawing.Drawing2D; using System.Windows.Forms; +using BizHawk.Bizware.Graphics; + namespace BizHawk.Client.EmuHawk { /// @@ -38,7 +40,7 @@ public RetainedViewportPanel(bool doubleBuffer = false) SetStyle(ControlStyles.Opaque, true); SetStyle(ControlStyles.UserMouse, true); - SetBitmap(new Bitmap(2, 2)); + SetBitmap(BitmapBuffer.CreateBitmapObject(new(2, 2))); } diff --git a/src/BizHawk.Client.EmuHawk/Extensions/ControlExtensions.cs b/src/BizHawk.Client.EmuHawk/Extensions/ControlExtensions.cs index e21a523b0f8..1996eb0d39f 100644 --- a/src/BizHawk.Client.EmuHawk/Extensions/ControlExtensions.cs +++ b/src/BizHawk.Client.EmuHawk/Extensions/ControlExtensions.cs @@ -11,6 +11,7 @@ using System.Text; using System.Windows.Forms; +using BizHawk.Bizware.Graphics; using BizHawk.Client.Common; using BizHawk.Common; using BizHawk.Common.CollectionExtensions; @@ -318,7 +319,7 @@ public static void Set(this DragEventArgs e, DragDropEffects effect) public static Bitmap ToBitMap(this Control control) { - var b = new Bitmap(control.Width, control.Height); + var b = BitmapBuffer.CreateBitmapObject(control.Size); var rect = new Rectangle(new Point(0, 0), control.Size); control.DrawToBitmap(b, rect); return b; diff --git a/src/BizHawk.Client.EmuHawk/FormBase.cs b/src/BizHawk.Client.EmuHawk/FormBase.cs index 2dc4319dce4..21df49c5e98 100644 --- a/src/BizHawk.Client.EmuHawk/FormBase.cs +++ b/src/BizHawk.Client.EmuHawk/FormBase.cs @@ -4,6 +4,7 @@ using System.Drawing; using System.Windows.Forms; +using BizHawk.Bizware.Graphics; using BizHawk.Client.Common; using BizHawk.Common; @@ -16,7 +17,7 @@ public class FormBase : Form /// removes transparency from an image by combining it with a solid background public static Image FillImageBackground(Image img, Color c) { - Bitmap result = new(width: img.Width, height: img.Height); + var result = BitmapBuffer.CreateBitmapObject(img.Size); using var g = Graphics.FromImage(result); g.Clear(c); g.DrawImage(img, x: 0, y: 0, width: img.Width, height: img.Height); diff --git a/src/BizHawk.Client.EmuHawk/MainForm.cs b/src/BizHawk.Client.EmuHawk/MainForm.cs index 2fd399af341..d90807a9a32 100644 --- a/src/BizHawk.Client.EmuHawk/MainForm.cs +++ b/src/BizHawk.Client.EmuHawk/MainForm.cs @@ -3353,7 +3353,7 @@ private void RecordAvBase(string videoWriterName, string filename, bool unattend bbIn.DiscardAlpha(); - Bitmap bmpOut = new(width: Config.AVWriterResizeWidth, height: Config.AVWriterResizeHeight, PixelFormat.Format32bppArgb); + var bmpOut = BitmapBuffer.CreateBitmapObject(new(width: Config.AVWriterResizeWidth, height: Config.AVWriterResizeHeight)); bmpIn = bbIn.ToSysdrawingBitmap(); using (var g = Graphics.FromImage(bmpOut)) { diff --git a/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevosAchievementForm.cs b/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevosAchievementForm.cs index e17ea086734..797cc27c200 100644 --- a/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevosAchievementForm.cs +++ b/src/BizHawk.Client.EmuHawk/RetroAchievements/RCheevosAchievementForm.cs @@ -2,6 +2,8 @@ using System.Drawing.Drawing2D; using System.Windows.Forms; +using BizHawk.Bizware.Graphics; + namespace BizHawk.Client.EmuHawk { /// @@ -43,7 +45,7 @@ public RCheevosAchievementForm(RCheevos.Cheevo cheevo, Func getChe private static Bitmap UpscaleBadge(Bitmap src) { - var ret = new Bitmap(120, 120); + var ret = BitmapBuffer.CreateBitmapObject(new(120, 120)); using var g = Graphics.FromImage(ret); g.InterpolationMode = InterpolationMode.NearestNeighbor; g.PixelOffsetMode = PixelOffsetMode.Half; diff --git a/src/BizHawk.Client.EmuHawk/config/GB/BmpView.cs b/src/BizHawk.Client.EmuHawk/config/GB/BmpView.cs index 8cff7e8ca0c..17cf7d19bb4 100644 --- a/src/BizHawk.Client.EmuHawk/config/GB/BmpView.cs +++ b/src/BizHawk.Client.EmuHawk/config/GB/BmpView.cs @@ -5,6 +5,8 @@ using System.Drawing; using System.Windows.Forms; +using BizHawk.Bizware.Graphics; + namespace BizHawk.Client.EmuHawk { public class BmpView : Control @@ -81,7 +83,7 @@ public void ChangeBitmapSize(int w, int h) } - Bmp = new Bitmap(w, h, PixelFormat.Format32bppArgb); + Bmp = BitmapBuffer.CreateBitmapObject(new(width: w, height: h)); BmpView_SizeChanged(null, EventArgs.Empty); Refresh(); } diff --git a/src/BizHawk.Client.EmuHawk/config/NES/NESGraphicsConfig.cs b/src/BizHawk.Client.EmuHawk/config/NES/NESGraphicsConfig.cs index e2054f776c1..c780d90345d 100644 --- a/src/BizHawk.Client.EmuHawk/config/NES/NESGraphicsConfig.cs +++ b/src/BizHawk.Client.EmuHawk/config/NES/NESGraphicsConfig.cs @@ -1,6 +1,7 @@ using System.Drawing; using System.Windows.Forms; +using BizHawk.Bizware.Graphics; using BizHawk.Common; using BizHawk.Client.Common; using BizHawk.Emulation.Common; @@ -75,7 +76,7 @@ private void SetPaletteImage() int w = pictureBoxPalette.Size.Width; int h = pictureBoxPalette.Size.Height; - var bmp = new Bitmap(w, h); + var bmp = BitmapBuffer.CreateBitmapObject(pictureBoxPalette.Size); for (int j = 0; j < h; j++) { int cy = j * 4 / h; diff --git a/src/BizHawk.Client.EmuHawk/config/NES/QuickNesConfig.cs b/src/BizHawk.Client.EmuHawk/config/NES/QuickNesConfig.cs index 790bad4c63e..cca6a4642f4 100644 --- a/src/BizHawk.Client.EmuHawk/config/NES/QuickNesConfig.cs +++ b/src/BizHawk.Client.EmuHawk/config/NES/QuickNesConfig.cs @@ -1,6 +1,7 @@ using System.Drawing; using System.Windows.Forms; +using BizHawk.Bizware.Graphics; using BizHawk.Emulation.Cores.Consoles.Nintendo.QuickNES; using BizHawk.Client.Common; using BizHawk.Common; @@ -38,7 +39,7 @@ private void SetPaletteImage() { int w = pictureBox1.Size.Width; int h = pictureBox1.Size.Height; - var bmp = new Bitmap(w, h); + var bmp = BitmapBuffer.CreateBitmapObject(pictureBox1.Size); var pal = _settings.Palette; for (int j = 0; j < h; j++) diff --git a/src/BizHawk.Client.EmuHawk/tools/GB/GBPrinterView.cs b/src/BizHawk.Client.EmuHawk/tools/GB/GBPrinterView.cs index ce543bc3330..9cd4a30cf8b 100644 --- a/src/BizHawk.Client.EmuHawk/tools/GB/GBPrinterView.cs +++ b/src/BizHawk.Client.EmuHawk/tools/GB/GBPrinterView.cs @@ -1,8 +1,11 @@ using System.Drawing; using System.Drawing.Imaging; +using System.Runtime.InteropServices; using System.Windows.Forms; +using BizHawk.Bizware.Graphics; using BizHawk.Client.Common; +using BizHawk.Common; using BizHawk.Emulation.Common; using BizHawk.Emulation.Cores.Consoles.Nintendo.Gameboy; @@ -86,9 +89,19 @@ private void OnPrint(IntPtr image, byte height, byte topMargin, byte bottomMargi // In this implementation: // the bottom margin and top margin are just white lines at the top and bottom // exposure is ignored + // height of 0 is treated as a height of 1 but zeroed data + var shouldFreePointer = false; + if (height is 0) + { + var sizeBytes = PaperWidth * sizeof(uint); + image = Marshal.AllocCoTaskMem(sizeBytes); + shouldFreePointer = true; + Util.UnsafeSpanFromPointer(image, length: sizeBytes).Fill(0); + height = 1; + } // The page received image - var page = new Bitmap(PaperWidth, height); + var page = BitmapBuffer.CreateBitmapObject(new(width: PaperWidth, height: height)); var bmp = page.LockBits(new Rectangle(0, 0, PaperWidth, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); @@ -107,6 +120,7 @@ private void OnPrint(IntPtr image, byte height, byte topMargin, byte bottomMargi } } + if (shouldFreePointer) Marshal.FreeCoTaskMem(image); page.UnlockBits(bmp); // add it to the bottom of the history @@ -147,7 +161,7 @@ private void ClearPaper() private void ResizeHistory(int height) { // copy to a new image of height - var newHistory = new Bitmap(PaperWidth, height); + var newHistory = BitmapBuffer.CreateBitmapObject(new(width: PaperWidth, height: height)); using (var g = Graphics.FromImage(newHistory)) { g.Clear(Color.FromArgb((int)PaperColor)); diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/LuaCanvas.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/LuaCanvas.cs index 8b103066604..199be91e5b3 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/LuaCanvas.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/LuaCanvas.cs @@ -3,6 +3,7 @@ using System.Windows.Forms; using System.IO; +using BizHawk.Bizware.Graphics; using BizHawk.Client.Common; using BizHawk.Common.PathExtensions; @@ -74,7 +75,7 @@ public LuaCanvas( // was this done after reflow for a reason? --yoshi luaPictureBox.Width = width; luaPictureBox.Height = height; - luaPictureBox.Image = new Bitmap(width, height); + luaPictureBox.Image = BitmapBuffer.CreateBitmapObject(luaPictureBox.Size); PerformLayout(); } diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/LuaPictureBox.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/LuaPictureBox.cs index 3e1ba9268ae..867e8ac503c 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/LuaPictureBox.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/LuaPictureBox.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Windows.Forms; +using BizHawk.Bizware.Graphics; using BizHawk.Client.Common; using BizHawk.Common.CollectionExtensions; @@ -40,7 +41,7 @@ private Pen GetPen([LuaColorParam] object color) public LuaPictureBox(NLuaTableHelper tableHelper, Action logOutputCallback) { - Image = new Bitmap(Width, Height); + Image = BitmapBuffer.CreateBitmapObject(Size); LogOutputCallback = logOutputCallback; TableHelper = tableHelper; } @@ -49,7 +50,7 @@ public void LuaResize(int width, int height) { Width = width; Height = height; - Image = new Bitmap(width, height); + Image = BitmapBuffer.CreateBitmapObject(Size); } public void Clear([LuaColorParam] object color) diff --git a/src/BizHawk.Client.EmuHawk/tools/NES/NESPPU.cs b/src/BizHawk.Client.EmuHawk/tools/NES/NESPPU.cs index 80b6cd540bd..6b9a92e6458 100644 --- a/src/BizHawk.Client.EmuHawk/tools/NES/NESPPU.cs +++ b/src/BizHawk.Client.EmuHawk/tools/NES/NESPPU.cs @@ -2,6 +2,7 @@ using System.Drawing.Imaging; using System.Windows.Forms; +using BizHawk.Bizware.Graphics; using BizHawk.Client.Common; using BizHawk.Emulation.Cores.Nintendo.NES; using BizHawk.Emulation.Common; @@ -24,7 +25,7 @@ public static Icon ToolIcon private int _scanline; - private Bitmap _zoomBoxDefaultImage = new Bitmap(64, 64); + private Bitmap _zoomBoxDefaultImage = BitmapBuffer.CreateBitmapObject(new(64, 64)); private bool _forceChange; [RequiredService] @@ -300,7 +301,7 @@ private void UpdatePaletteSelection() private static Bitmap Section(Image srcBitmap, Rectangle section, bool is8x16) { // Create the new bitmap and associated graphics object - var bmp = new Bitmap(64, 64); + var bmp = BitmapBuffer.CreateBitmapObject(new(64, 64)); var g = Graphics.FromImage(bmp); // Draw the specified section of the source bitmap to the new one @@ -473,7 +474,7 @@ private void SpriteRefreshMenuItem_Click(object sender, EventArgs e) private void NesPPU_MouseClick(object sender, MouseEventArgs e) { - ZoomBox.Image = new Bitmap(64, 64); + ZoomBox.Image = BitmapBuffer.CreateBitmapObject(new(64, 64)); } private void NesPPU_KeyDown(object sender, KeyEventArgs e) @@ -666,7 +667,7 @@ private void HandlePaletteViewMouseMove(Point e) AddressLabel.Text = $"Address: 0x{addr:X4}"; int val; - var bmp = new Bitmap(64, 64); + var bmp = BitmapBuffer.CreateBitmapObject(new(64, 64)); var g = Graphics.FromImage(bmp); byte[] palRam = _ppu.GetPalRam(); diff --git a/src/BizHawk.Client.EmuHawk/tools/NES/NameTableViewer.cs b/src/BizHawk.Client.EmuHawk/tools/NES/NameTableViewer.cs index cd8c58121a1..5249f1f66a4 100644 --- a/src/BizHawk.Client.EmuHawk/tools/NES/NameTableViewer.cs +++ b/src/BizHawk.Client.EmuHawk/tools/NES/NameTableViewer.cs @@ -1,6 +1,8 @@ using System.Drawing; using System.Windows.Forms; +using BizHawk.Bizware.Graphics; + namespace BizHawk.Client.EmuHawk { public sealed class NameTableViewer : Control @@ -10,7 +12,7 @@ public sealed class NameTableViewer : Control public NameTableViewer() { var pSize = new Size(512, 480); - Nametables = new Bitmap(pSize.Width, pSize.Height); + Nametables = BitmapBuffer.CreateBitmapObject(pSize); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); @@ -70,7 +72,7 @@ private void NameTableViewer_Paint(object sender, PaintEventArgs e) public void ScreenshotToClipboard() { - using var b = new Bitmap(Width, Height); + using var b = BitmapBuffer.CreateBitmapObject(Size); var rect = new Rectangle(new Point(0, 0), Size); DrawToBitmap(b, rect); Clipboard.SetImage(b); diff --git a/src/BizHawk.Client.EmuHawk/tools/NES/PaletteViewer.cs b/src/BizHawk.Client.EmuHawk/tools/NES/PaletteViewer.cs index fdad30c197e..1e1c783b8d6 100644 --- a/src/BizHawk.Client.EmuHawk/tools/NES/PaletteViewer.cs +++ b/src/BizHawk.Client.EmuHawk/tools/NES/PaletteViewer.cs @@ -1,6 +1,8 @@ using System.Drawing; using System.Windows.Forms; +using BizHawk.Bizware.Graphics; + namespace BizHawk.Client.EmuHawk { public sealed class PaletteViewer : Control @@ -76,7 +78,7 @@ public bool HasChanged() public void ScreenshotToClipboard() { - var b = new Bitmap(Width, Height); + var b = BitmapBuffer.CreateBitmapObject(Size); var rect = new Rectangle(new Point(0, 0), Size); DrawToBitmap(b, rect); diff --git a/src/BizHawk.Client.EmuHawk/tools/NES/PatternViewer.cs b/src/BizHawk.Client.EmuHawk/tools/NES/PatternViewer.cs index 78b6645c73b..63525c00565 100644 --- a/src/BizHawk.Client.EmuHawk/tools/NES/PatternViewer.cs +++ b/src/BizHawk.Client.EmuHawk/tools/NES/PatternViewer.cs @@ -1,6 +1,8 @@ using System.Drawing; using System.Windows.Forms; +using BizHawk.Bizware.Graphics; + namespace BizHawk.Client.EmuHawk { public sealed class PatternViewer : Control @@ -12,7 +14,7 @@ public sealed class PatternViewer : Control public PatternViewer() { var pSize = new Size(256, 128); - Pattern = new Bitmap(pSize.Width, pSize.Height); + Pattern = BitmapBuffer.CreateBitmapObject(pSize); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); @@ -32,7 +34,7 @@ private void PatternViewer_Paint(object sender, PaintEventArgs e) public void ScreenshotToClipboard() { - var b = new Bitmap(Width, Height); + var b = BitmapBuffer.CreateBitmapObject(Size); var rect = new Rectangle(new Point(0, 0), Size); DrawToBitmap(b, rect); diff --git a/src/BizHawk.Client.EmuHawk/tools/NES/SpriteViewer.cs b/src/BizHawk.Client.EmuHawk/tools/NES/SpriteViewer.cs index f61c65e0226..bda61d340bf 100644 --- a/src/BizHawk.Client.EmuHawk/tools/NES/SpriteViewer.cs +++ b/src/BizHawk.Client.EmuHawk/tools/NES/SpriteViewer.cs @@ -1,6 +1,8 @@ using System.Drawing; using System.Windows.Forms; +using BizHawk.Bizware.Graphics; + namespace BizHawk.Client.EmuHawk { public sealed class SpriteViewer : Control @@ -11,7 +13,7 @@ public SpriteViewer() { SetStyle(ControlStyles.SupportsTransparentBackColor, true); var pSize = new Size(256, 96); - Sprites = new Bitmap(pSize.Width, pSize.Height); + Sprites = BitmapBuffer.CreateBitmapObject(pSize); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.DoubleBuffer, true); @@ -34,7 +36,7 @@ private void SpriteViewer_Paint(object sender, PaintEventArgs e) public void ScreenshotToClipboard() { - var b = new Bitmap(Width, Height); + var b = BitmapBuffer.CreateBitmapObject(Size); var rect = new Rectangle(new Point(0, 0), Size); DrawToBitmap(b, rect); diff --git a/src/BizHawk.Client.EmuHawk/tools/PCE/PCEBGCanvas.cs b/src/BizHawk.Client.EmuHawk/tools/PCE/PCEBGCanvas.cs index 9481b1c302d..8a7e4be204b 100644 --- a/src/BizHawk.Client.EmuHawk/tools/PCE/PCEBGCanvas.cs +++ b/src/BizHawk.Client.EmuHawk/tools/PCE/PCEBGCanvas.cs @@ -1,6 +1,7 @@ using System.Drawing; using System.Windows.Forms; -using System.Drawing.Imaging; + +using BizHawk.Bizware.Graphics; namespace BizHawk.Client.EmuHawk { @@ -13,11 +14,11 @@ public class PceBgCanvas : Control public PceBgCanvas() { - Bat = new Bitmap(BAT_WIDTH, BAT_HEIGHT, PixelFormat.Format32bppArgb); + Size = new(BAT_WIDTH, BAT_HEIGHT); + Bat = BitmapBuffer.CreateBitmapObject(Size); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.DoubleBuffer, true); - Size = new Size(BAT_WIDTH, BAT_HEIGHT); Paint += BGViewer_Paint; } diff --git a/src/BizHawk.Client.EmuHawk/tools/SNES/SNESGraphicsDebugger.cs b/src/BizHawk.Client.EmuHawk/tools/SNES/SNESGraphicsDebugger.cs index 763243cdb8b..927e9a0ba99 100644 --- a/src/BizHawk.Client.EmuHawk/tools/SNES/SNESGraphicsDebugger.cs +++ b/src/BizHawk.Client.EmuHawk/tools/SNES/SNESGraphicsDebugger.cs @@ -29,6 +29,7 @@ using System.Drawing.Imaging; using System.Windows.Forms; +using BizHawk.Bizware.Graphics; using BizHawk.Client.Common; using BizHawk.Emulation.Cores.Nintendo.SNES; using BizHawk.Emulation.Common; @@ -346,7 +347,7 @@ private void RenderView() Action allocate = (w, h) => { - bmp = new Bitmap(w, h); + bmp = BitmapBuffer.CreateBitmapObject(new(width: w, height: h)); bmpdata = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); pixelptr = (int*)bmpdata.Scan0.ToPointer(); stride = bmpdata.Stride; @@ -670,7 +671,7 @@ private void RenderPalette() int pixsize = paletteCellSize * 16 + paletteCellSpacing * 17; int cellTotalSize = (paletteCellSize + paletteCellSpacing); - var bmp = new Bitmap(pixsize, pixsize, PixelFormat.Format32bppArgb); + var bmp = BitmapBuffer.CreateBitmapObject(new(pixsize, pixsize)); using (var g = Graphics.FromImage(bmp)) { using SolidBrush brush = new(default); @@ -959,7 +960,7 @@ private void RenderTileView() int tileSize = si.BG[bgs.bgnum].TileSize; int pixels = tileSize * tileSize; - var bmp = new Bitmap(tileSize, tileSize, PixelFormat.Format32bppArgb); + var bmp = BitmapBuffer.CreateBitmapObject(new(tileSize, tileSize)); var bmpData = bmp.LockBits(new Rectangle(0, 0, tileSize, tileSize), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); if (viewBgMode == SNESGraphicsDecoder.BGMode.Mode7) @@ -983,7 +984,7 @@ private void RenderTileView() //view a tileset tile int bpp = currTileDataState.Bpp; - var bmp = new Bitmap(8, 8, PixelFormat.Format32bppArgb); + var bmp = BitmapBuffer.CreateBitmapObject(new(8, 8)); var bmpdata = bmp.LockBits(new Rectangle(0, 0, 8, 8), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); if (currTileDataState.Type == eDisplayType.TilesMode7) gd.RenderMode7TilesToScreen((int*)bmpdata.Scan0, bmpdata.Stride / 4, false, false, 1, currTileDataState.Tile, 1); @@ -1007,7 +1008,7 @@ private void RenderTileView() var bounds = si.ObjSizeBoundsSquare; int width = bounds.Width; int height = bounds.Height; - var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb); + var bmp = BitmapBuffer.CreateBitmapObject(bounds); var bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); gd.RenderSpriteToScreen((int*)bmpData.Scan0, bmpData.Stride / 4, 0, 0, si, currObjDataState.Number); bmp.UnlockBits(bmpData); @@ -1015,7 +1016,7 @@ private void RenderTileView() } else { - var bmp = new Bitmap(8, 8, PixelFormat.Format32bppArgb); + var bmp = BitmapBuffer.CreateBitmapObject(new(8, 8)); viewerTile.SetBitmap(bmp); } } diff --git a/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/components/AnalogStickPanel.cs b/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/components/AnalogStickPanel.cs index d8336a9cf2f..bacbab96eba 100644 --- a/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/components/AnalogStickPanel.cs +++ b/src/BizHawk.Client.EmuHawk/tools/VirtualPads/controls/components/AnalogStickPanel.cs @@ -1,6 +1,8 @@ using System.ComponentModel; using System.Drawing; using System.Windows.Forms; + +using BizHawk.Bizware.Graphics; using BizHawk.Client.Common; using BizHawk.Common; using BizHawk.Common.NumberExtensions; @@ -137,8 +139,9 @@ private int GfxToRealY(int val) => private readonly Pen _bluePen = new Pen(Brushes.Blue, 2); private readonly Pen _grayPen = new Pen(Brushes.Gray, 2); - private readonly Bitmap _dot = new Bitmap(7, 7); - private readonly Bitmap _grayDot = new Bitmap(7, 7); + private readonly Bitmap _dot = BitmapBuffer.CreateBitmapObject(new(7, 7)); + + private readonly Bitmap _grayDot = BitmapBuffer.CreateBitmapObject(new(7, 7)); [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public Action ClearCallback { get; set; }