How to enable (change color) one pixel by coordinates in WPF


I saw similar questions here, but could not find an answer.
I take a computer graphics course at college, and we are taught various algorithms that are used to display shapes. My task is to choose any development platform and implement these algorithms.
Since I have development experience in WPF, I want to use it for this purpose.
But I can not find how to give the coordinates of the pixel and change its color.
I know that school-related questions are not so popular on stackoverflow here, but I don’t feel that asking this question is deceiving my homework anyway.
Thanks!

+6
pixel wpf graphics
source share
3 answers

You have three options:

  • Add a 1px rectangle to the Canvas (the canvas is how you coordinate the position in WPF)
  • Make some custom picture in WriteableBitmap (examples on this page)
  • Make some custom picture in the CompositionTarget.Rendering event and β€œopen” the renderer as follows:

    using (DrawingContext context = visual.RenderOpen()) { context.DrawRectangle(Brushes.Red, null, new Rect(5,5,1,1)); } 
+5
source share

Try a look at the WriteableBitmap class. WPF does not allow you to directly access pixels, but WriteableBitmap allows you to set pixels on bitmaps and then render them.

+1
source share

You can use a Shape object, such as Line or Rectangle, in XAML or in code.

For example, using a string in XAML, you can use

 <Line X1="10" Y1="10" X2="11" Y2="11" Stroke="Black" StrokeThickness="1" /> 

X1 is the initial coordinate x. X2 is the coordinate of the end x. Y1 is the initial y coordinate. Y2 is the final y coordinate.

0
source share

All Articles