Cyclonit
Cyclonit
CC#
Created by Cyclonit on 6/20/2023 in #help
❔ Refresh WriteableBitmap
Hi, I would like to use a WriteableBitmap in my application. However, the Image control does not update when the backing WriteableBitmap changes. The examples I could find online appear to use WriteableBitmap.AddDirtyRect to trigger the update. But in my case this does not nothing. My control looks like this:
<UserControl ... />
<Image x:Name="bitmap"
Source="{Binding Bitmap}"
Opacity=".25">
</Image>
</UserControl>
<UserControl ... />
<Image x:Name="bitmap"
Source="{Binding Bitmap}"
Opacity=".25">
</Image>
</UserControl>
And the relevant code within the ViewModel:
public class BitGridViewModel : ObservableBase
{
private static readonly Color trueColor = Color.FromArgb(0xAA, 0xFF, 0x00, 0x00);
private static readonly Color falseColor = Color.FromArgb(0xAA, 0xFF, 0xFF, 0xFF);

public WriteableBitmap Bitmap
{
get => _bitmap;
set => SetProperty(ref _bitmap, value);
}
private WriteableBitmap _bitmap;

...

// https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.imaging.writeablebitmap?view=windowsdesktop-7.0
private static void DrawPixel(WriteableBitmap bitmap, int x, int y, Color color)
{
if (bitmap.Format != PixelFormats.Bgr32)
throw new ArgumentException();

try
{
bitmap.Lock();

unsafe
{
// determine the pixel's address within the Bitmap, depends on the Bitmap's PixelFormat
IntPtr pBackBufferAddr = bitmap.BackBuffer;
IntPtr pixelAddr = pBackBufferAddr + y * bitmap.BackBufferStride + x * 4;

// compute the pixel's color, depends on the Bitmap's PixelFormat
int colorData = (color.R << 16) | (color.G << 8) | (color.B << 0);

// set the pixel
*((int*)pixelAddr) = colorData;
}

bitmap.AddDirtyRect(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight));
}
finally
{
bitmap.Unlock();
}
}
}
public class BitGridViewModel : ObservableBase
{
private static readonly Color trueColor = Color.FromArgb(0xAA, 0xFF, 0x00, 0x00);
private static readonly Color falseColor = Color.FromArgb(0xAA, 0xFF, 0xFF, 0xFF);

public WriteableBitmap Bitmap
{
get => _bitmap;
set => SetProperty(ref _bitmap, value);
}
private WriteableBitmap _bitmap;

...

// https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.imaging.writeablebitmap?view=windowsdesktop-7.0
private static void DrawPixel(WriteableBitmap bitmap, int x, int y, Color color)
{
if (bitmap.Format != PixelFormats.Bgr32)
throw new ArgumentException();

try
{
bitmap.Lock();

unsafe
{
// determine the pixel's address within the Bitmap, depends on the Bitmap's PixelFormat
IntPtr pBackBufferAddr = bitmap.BackBuffer;
IntPtr pixelAddr = pBackBufferAddr + y * bitmap.BackBufferStride + x * 4;

// compute the pixel's color, depends on the Bitmap's PixelFormat
int colorData = (color.R << 16) | (color.G << 8) | (color.B << 0);

// set the pixel
*((int*)pixelAddr) = colorData;
}

bitmap.AddDirtyRect(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight));
}
finally
{
bitmap.Unlock();
}
}
}
9 replies
CC#
Created by Cyclonit on 5/29/2023 in #help
❔ WPF: draw polygen offset from origin
2 replies
CC#
Created by Cyclonit on 1/16/2023 in #help
❔ load .dds to BitmapImage
Hi, I am working on a small WPF application for a game. The game's icons are stored as .dds files and I need to display them on a canvas. I am familiar with loading .pngs as streams and creating a BitmapImage from them, but I am unable to figure out how to do it with .dds. As far as I am aware, WPF does not support .dds by itself. Thus I checked out some libraries and found pfim. Loading the image works fine and the data appears to make sense, but I fail at converting the Pfimage into a BitmapImage for WPF. The official example uses an older version of net with Bitmap instead of BitmapImage. All other solutions/guides I could find are outdated too. They either use outdated libraries, versions of C# or both. Can someone help me figure out a solution to this?
2 replies