Skip to content

Commit 6b1f0e0

Browse files
author
Wayfarer
committed
Tried to add a new converter but still fails me.
I should take a better look next time.
1 parent 8ff3418 commit 6b1f0e0

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

Imaging/DirectBitmapImage.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@ public DirectBitmapImage(int width, int height)
7373
_bitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgra32, null);
7474
}
7575

76+
/// <summary>
77+
/// Constructs a DirectBitmapImage from a pixel array and width.
78+
/// Assumes BGRA32 format and calculates height automatically.
79+
/// </summary>
80+
/// <param name="bits">The pixel array (BGRA32).</param>
81+
/// <param name="width">The width of the image.</param>
82+
public DirectBitmapImage(Pixel32[] bits, int width)
83+
{
84+
if (bits == null) throw new ArgumentNullException(nameof(bits));
85+
if (width <= 0) throw new ArgumentOutOfRangeException(nameof(width));
86+
if (bits.Length % width != 0) throw new ArgumentException("Pixel array length must be divisible by width.");
87+
88+
int height = bits.Length / width;
89+
Bits = bits;
90+
_bitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgra32, null);
91+
}
92+
7693
/// <summary>
7794
/// The height of the image.
7895
/// </summary>
@@ -153,7 +170,7 @@ public void SetPixelsUnsafe(IEnumerable<PixelData> pixels)
153170
/// Fills the bitmap with a uniform color using SIMD.
154171
/// </summary>
155172
/// <param name="color">The color to fill with.</param>
156-
public unsafe void FillSimd(Color color)
173+
public unsafe void FillSimd(System.Drawing.Color color)
157174
{
158175
var packed = (uint)(color.A << 24 | color.R << 16 | color.G << 8 | color.B);
159176

Imaging/ImageStreamMedia.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,28 @@ internal static BitmapImage BitmapToBitmapImage(Bitmap image, bool lossless = fa
178178
}
179179
}
180180

181+
/// <summary>
182+
/// Converts a System.Drawing <see cref="Bitmap"/> to a WPF <see cref="BitmapImage"/>.
183+
/// </summary>
184+
/// <param name="image">The source bitmap.</param>
185+
/// <returns>A <see cref="BitmapImage"/>.</returns>
186+
/// <exception cref="ArgumentNullException"></exception>
187+
//internal static BitmapImage BitmapToBitmapImage(Bitmap image)
188+
//{
189+
// ImageHelper.ValidateImage(nameof(BitmapToBitmapImage), image);
190+
191+
// // 1. Create the GDI+ wrapper.
192+
// // This constructor draws the source into a NEW Pixel32[] array automatically.
193+
// using var gdiBackend = new DirectBitmap(image);
194+
195+
// // 2. Wrap the SAME array in the WPF class.
196+
// // Use the constructor you just showed me.
197+
// var wpfFrontend = new DirectBitmapImage(gdiBackend.Bits, gdiBackend.Width);
198+
199+
// // 3. Prepare for WPF UI
200+
// return wpfFrontend.BitmapImage;
201+
//}
202+
181203
/// <summary>
182204
/// Fast conversion using WriteableBitmap.
183205
/// </summary>

0 commit comments

Comments
 (0)