Click here to Skip to main content
15,890,579 members
Home / Discussions / WPF
   

WPF

 
QuestionWorking with 3D models created in Blender Pin
Etienne_12312-Mar-09 0:11
Etienne_12312-Mar-09 0:11 
AnswerRe: Working with 3D models created in Blender Pin
Pete O'Hanlon12-Mar-09 0:27
mvePete O'Hanlon12-Mar-09 0:27 
QuestionWPF: How to make resizing custom datagrid column? [modified] Pin
MichalDawn11-Mar-09 22:58
MichalDawn11-Mar-09 22:58 
AnswerRe: WPF: How to make resizing custom datagrid column? Pin
ABitSmart11-Mar-09 23:40
ABitSmart11-Mar-09 23:40 
GeneralRe: WPF: How to make resizing custom datagrid column? Pin
MichalDawn11-Mar-09 23:51
MichalDawn11-Mar-09 23:51 
GeneralRe: WPF: How to make resizing custom datagrid column? Pin
ABitSmart11-Mar-09 23:53
ABitSmart11-Mar-09 23:53 
GeneralRe: WPF: How to make resizing custom datagrid column? [modified] Pin
MichalDawn12-Mar-09 0:02
MichalDawn12-Mar-09 0:02 
QuestionHow to render pixels of a bitmap correctly? Pin
Andy@11-Mar-09 5:03
Andy@11-Mar-09 5:03 
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 and very small.

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.


I used the following code in my control:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

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);
            }
        }
    }
}


Thanks,
Andy
AnswerRe: How to render pixels of a bitmap correctly? Pin
Insincere Dave11-Mar-09 16:14
Insincere Dave11-Mar-09 16:14 
GeneralRe: How to render pixels of a bitmap correctly? Pin
Andy@12-Mar-09 2:59
Andy@12-Mar-09 2:59 
GeneralRe: How to render pixels of a bitmap correctly? Pin
Insincere Dave12-Mar-09 6:56
Insincere Dave12-Mar-09 6:56 
GeneralRe: How to render pixels of a bitmap correctly? Pin
Andy@17-Mar-09 4:20
Andy@17-Mar-09 4:20 
QuestionBind an attached property Pin
Pauwels Bart11-Mar-09 3:55
Pauwels Bart11-Mar-09 3:55 
AnswerRe: Bind an attached property Pin
ABitSmart11-Mar-09 16:57
ABitSmart11-Mar-09 16:57 
QuestionRe: Bind an attached property Pin
Pauwels Bart11-Mar-09 19:45
Pauwels Bart11-Mar-09 19:45 
AnswerRe: Bind an attached property Pin
ABitSmart11-Mar-09 21:00
ABitSmart11-Mar-09 21:00 
QuestionRe: Bind an attached property Pin
Pauwels Bart11-Mar-09 22:33
Pauwels Bart11-Mar-09 22:33 
AnswerRe: Bind an attached property Pin
ABitSmart11-Mar-09 23:13
ABitSmart11-Mar-09 23:13 
GeneralRe: Bind an attached property Pin
Pauwels Bart12-Mar-09 0:54
Pauwels Bart12-Mar-09 0:54 
QuestionCan a WPF browser app be hosted on a server and accessed multiply by other users Pin
DimitarS10-Mar-09 21:56
DimitarS10-Mar-09 21:56 
AnswerRe: Can a WPF browser app be hosted on a server and accessed multiply by other users Pin
Pete O'Hanlon10-Mar-09 22:34
mvePete O'Hanlon10-Mar-09 22:34 
GeneralRe: Can a WPF browser app be hosted on a server and accessed multiply by other users Pin
DimitarS10-Mar-09 22:36
DimitarS10-Mar-09 22:36 
QuestionDecoupling WCF client proxy Pin
Mark J. Miller10-Mar-09 14:02
Mark J. Miller10-Mar-09 14:02 
QuestionTreeView vertical scrollbar bug Pin
Steve The Plant10-Mar-09 10:26
Steve The Plant10-Mar-09 10:26 
AnswerRe: TreeView vertical scrollbar bug Pin
ABitSmart10-Mar-09 17:23
ABitSmart10-Mar-09 17:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.