A
a
Hello,
i´m trying to write a WPF User Control that looks like a matrix of LEDs.
I´ve created a RenderTargetBitmap over a DrawingVisual that looks like a
matrix of quadratic dots.
The problem is that, when using the control, the dots are not displayed
quadratic. The dots are displayed rectangular instead.
I´ve also saved the created matrix bitmap in a bmp file. The matrix bitmap
is correctly displayed if it is opened with a standard program.
How can i change rendering, so that the pixel of the matrix bitmap are
displayed correctly?
In the past i´ve created such a matrix bitmap with MFC and BitBlt, ther was
no such blur, if the bitmap was displayed by a control.
Or is there another approach to create such a LED matrix?
I´ve also tried to create a shape element for each LED, but the CPU usage
was very high, by updating the LEDs every 200 or 500 milli seconds.
I used the following code in my control:
using System.IO;
namespace TestWpf
{
/// <summary>
/// Interaction logic for MyControl.xaml
/// </summary>
public partial class MyControl : UserControl
{
private const double INCH_PER_UNIT = 1.0 / 96.0;
private const int BMP_DPI = 96;
private const double PIXEL_RAD = 1;
private const double PIXEL_SPACE = 1;
private static readonly Brush PIXEL_OFF_BRUSH = new
SolidColorBrush(Color.FromRgb(150, 150, 150));
private static readonly Brush PIXEL_BACKGND_BRUSH = new
SolidColorBrush(Colors.Black);
RenderTargetBitmap _pixelOffBmp;
RenderTargetBitmap _matrixBmp;
public MyControl()
{
InitializeComponent();
_pixelOffBmp = CreatePixelBmp(PIXEL_OFF_BRUSH);
_matrixBmp = CreateMatrixBmp();
SaveBmp(_matrixBmp);
}
protected override void OnRender(DrawingContext drawingContext)
{
// base.OnRender(drawingContext);
drawingContext.DrawImage(_matrixBmp, new Rect(0, 0,
_matrixBmp.Width, _matrixBmp.Height));
}
RenderTargetBitmap CreatePixelBmp(Brush pixBrush)
{
double unitWidth, unitHeight; // 1/96th inch per unit
unitWidth = 2 * PIXEL_RAD;
unitHeight = unitWidth;
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext dc = drawingVisual.RenderOpen())
{
dc.DrawRectangle(pixBrush, null, new Rect(0, 0, unitWidth,
unitHeight));
}
int pixelWidth, pixelHeight;
pixelWidth = (int)(BMP_DPI * unitWidth * INCH_PER_UNIT);
pixelHeight = (int)(BMP_DPI * unitHeight * INCH_PER_UNIT);
RenderTargetBitmap bmp = new RenderTargetBitmap(pixelWidth,
pixelHeight, BMP_DPI, BMP_DPI, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);
bmp.Freeze();
return bmp;
}
RenderTargetBitmap CreateMatrixBmp()
{
int xMatrixPixel = 20 * 6;
int yMatrixPixel = 2 * 8;
double xUnitsMatrixBmp = xMatrixPixel * (2 * PIXEL_RAD +
PIXEL_SPACE) + PIXEL_SPACE; // 1/96th inch per unit
double yUnitsMatrixBmp = yMatrixPixel * (2 * PIXEL_RAD +
PIXEL_SPACE) +PIXEL_SPACE;
int pixelWidthMatrixBmp = (int)(BMP_DPI * xUnitsMatrixBmp *
INCH_PER_UNIT);
int pixelHeightMatrixBmp = (int)(BMP_DPI * yUnitsMatrixBmp *
INCH_PER_UNIT);
this.Width = xUnitsMatrixBmp + BorderThickness.Left +
BorderThickness.Right;
this.Height = yUnitsMatrixBmp + BorderThickness.Top +
BorderThickness.Bottom;
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext dc = drawingVisual.RenderOpen())
{
dc.DrawRectangle(PIXEL_BACKGND_BRUSH, null, new Rect(0, 0,
xUnitsMatrixBmp, yUnitsMatrixBmp));
double xOffset = 0;
double yOffset = PIXEL_SPACE;
for (int y = 0; y < yMatrixPixel; y++)
{
xOffset = PIXEL_SPACE;
for (int x = 0; x < xMatrixPixel; x++)
{
dc.DrawImage(_pixelOffBmp, new Rect(xOffset,
yOffset, _pixelOffBmp.Width, _pixelOffBmp.Height));
xOffset += _pixelOffBmp.Width + PIXEL_SPACE;
}
yOffset += _pixelOffBmp.Height + PIXEL_SPACE;
}
}
RenderTargetBitmap bmp = new
RenderTargetBitmap(pixelWidthMatrixBmp, pixelHeightMatrixBmp, BMP_DPI,
BMP_DPI, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);
bmp.Freeze();
return bmp;
}
void SaveBmp(BitmapSource bmpSource)
{
BmpBitmapEncoder bmp = new BmpBitmapEncoder();
bmp.Frames.Add(BitmapFrame.Create(bmpSource));
using (Stream stm = File.Create("foo.bmp"))
{
bmp.Save(stm);
}
}
}
}
Regards.
i´m trying to write a WPF User Control that looks like a matrix of LEDs.
I´ve created a RenderTargetBitmap over a DrawingVisual that looks like a
matrix of quadratic dots.
The problem is that, when using the control, the dots are not displayed
quadratic. The dots are displayed rectangular instead.
I´ve also saved the created matrix bitmap in a bmp file. The matrix bitmap
is correctly displayed if it is opened with a standard program.
How can i change rendering, so that the pixel of the matrix bitmap are
displayed correctly?
In the past i´ve created such a matrix bitmap with MFC and BitBlt, ther was
no such blur, if the bitmap was displayed by a control.
Or is there another approach to create such a LED matrix?
I´ve also tried to create a shape element for each LED, but the CPU usage
was very high, by updating the LEDs every 200 or 500 milli seconds.
I used the following code in my control:
using System.IO;
namespace TestWpf
{
/// <summary>
/// Interaction logic for MyControl.xaml
/// </summary>
public partial class MyControl : UserControl
{
private const double INCH_PER_UNIT = 1.0 / 96.0;
private const int BMP_DPI = 96;
private const double PIXEL_RAD = 1;
private const double PIXEL_SPACE = 1;
private static readonly Brush PIXEL_OFF_BRUSH = new
SolidColorBrush(Color.FromRgb(150, 150, 150));
private static readonly Brush PIXEL_BACKGND_BRUSH = new
SolidColorBrush(Colors.Black);
RenderTargetBitmap _pixelOffBmp;
RenderTargetBitmap _matrixBmp;
public MyControl()
{
InitializeComponent();
_pixelOffBmp = CreatePixelBmp(PIXEL_OFF_BRUSH);
_matrixBmp = CreateMatrixBmp();
SaveBmp(_matrixBmp);
}
protected override void OnRender(DrawingContext drawingContext)
{
// base.OnRender(drawingContext);
drawingContext.DrawImage(_matrixBmp, new Rect(0, 0,
_matrixBmp.Width, _matrixBmp.Height));
}
RenderTargetBitmap CreatePixelBmp(Brush pixBrush)
{
double unitWidth, unitHeight; // 1/96th inch per unit
unitWidth = 2 * PIXEL_RAD;
unitHeight = unitWidth;
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext dc = drawingVisual.RenderOpen())
{
dc.DrawRectangle(pixBrush, null, new Rect(0, 0, unitWidth,
unitHeight));
}
int pixelWidth, pixelHeight;
pixelWidth = (int)(BMP_DPI * unitWidth * INCH_PER_UNIT);
pixelHeight = (int)(BMP_DPI * unitHeight * INCH_PER_UNIT);
RenderTargetBitmap bmp = new RenderTargetBitmap(pixelWidth,
pixelHeight, BMP_DPI, BMP_DPI, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);
bmp.Freeze();
return bmp;
}
RenderTargetBitmap CreateMatrixBmp()
{
int xMatrixPixel = 20 * 6;
int yMatrixPixel = 2 * 8;
double xUnitsMatrixBmp = xMatrixPixel * (2 * PIXEL_RAD +
PIXEL_SPACE) + PIXEL_SPACE; // 1/96th inch per unit
double yUnitsMatrixBmp = yMatrixPixel * (2 * PIXEL_RAD +
PIXEL_SPACE) +PIXEL_SPACE;
int pixelWidthMatrixBmp = (int)(BMP_DPI * xUnitsMatrixBmp *
INCH_PER_UNIT);
int pixelHeightMatrixBmp = (int)(BMP_DPI * yUnitsMatrixBmp *
INCH_PER_UNIT);
this.Width = xUnitsMatrixBmp + BorderThickness.Left +
BorderThickness.Right;
this.Height = yUnitsMatrixBmp + BorderThickness.Top +
BorderThickness.Bottom;
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext dc = drawingVisual.RenderOpen())
{
dc.DrawRectangle(PIXEL_BACKGND_BRUSH, null, new Rect(0, 0,
xUnitsMatrixBmp, yUnitsMatrixBmp));
double xOffset = 0;
double yOffset = PIXEL_SPACE;
for (int y = 0; y < yMatrixPixel; y++)
{
xOffset = PIXEL_SPACE;
for (int x = 0; x < xMatrixPixel; x++)
{
dc.DrawImage(_pixelOffBmp, new Rect(xOffset,
yOffset, _pixelOffBmp.Width, _pixelOffBmp.Height));
xOffset += _pixelOffBmp.Width + PIXEL_SPACE;
}
yOffset += _pixelOffBmp.Height + PIXEL_SPACE;
}
}
RenderTargetBitmap bmp = new
RenderTargetBitmap(pixelWidthMatrixBmp, pixelHeightMatrixBmp, BMP_DPI,
BMP_DPI, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);
bmp.Freeze();
return bmp;
}
void SaveBmp(BitmapSource bmpSource)
{
BmpBitmapEncoder bmp = new BmpBitmapEncoder();
bmp.Frames.Add(BitmapFrame.Create(bmpSource));
using (Stream stm = File.Create("foo.bmp"))
{
bmp.Save(stm);
}
}
}
}
Regards.