4

I'm sorry for asking such a simple question but this is driving me nuts.

I'm writing in C#, WPF and all I need to do is draw a pixel at location x,y of a specific RGB value.

What I'm running afoul of is the namespaces to use, the dlls to reference, etc. Any code I find throws a ton of compile errors.

So just something short and sweet that will place a dot of RGB value whatever at a specific x,y coordinate in the window would really make my day.

Please include the entire example code that will compile, link and run in Visual Studio 2010. Like I said, all the code snippets I've found throw a ton of errors (everything from ambiguous 'Brush' to... well, you name it). And, yes, I'm just starting to write in WPF.

Thanks in advance!


This is for Surfbutler:

Here's an example (right from here: http://msdn.microsoft.com/en-us/library/ms747393.aspx):

// Add a Line Element
myLine = new Line();
myLine.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
myLine.X1 = 1;
myLine.X2 = 50;
myLine.Y1 = 1;
myLine.Y2 = 50;
myLine.HorizontalAlignment = HorizontalAlignment.Left;
myLine.VerticalAlignment = VerticalAlignment.Center;
myLine.StrokeThickness = 2;
myGrid.Children.Add(myLine);           

And it throws errors not knowing what a Line() is..or myGrid (would that be declared in the XAML?

Adam Lear
  • 38,111
  • 12
  • 81
  • 101
zetar
  • 1,225
  • 2
  • 20
  • 45
  • 2
    You may want this http://stackoverflow.com/questions/443867/drawing-pixels-in-wpf – Adil Oct 06 '12 at 00:46
  • @zetar: If you don't get a complete working example (and you may not), find a snippet you like, try to get it working, and if you can't, post a question on here (with your code as far as you have got) and we will try to help you get the rest of the way :) – Surfbutler Oct 06 '12 at 07:36
  • Here's an example (right from here: http://msdn.microsoft.com/en-us/library/ms747393.aspx): – zetar Oct 06 '12 at 14:25
  • Also it throws this error (I can't find what assembly to add): The type 'System.Windows.Freezable' is defined in an assembly that is not referenced. You must add a reference to assembly 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. – zetar Oct 06 '12 at 14:34
  • And this error, again, I don't know what assembly to add: The type 'System.Windows.DependencyObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. – zetar Oct 06 '12 at 14:36
  • 2
    msdn.microsoft.com is your friend. Here you can learn that Line belongs to System.Windows.Shapes. So using System.Windows.Shapes.Line instead of Line works, and soon you'll prefer typing "using System.Windows.Shapes" in your class... BTW if you have so much missing reference restart from scratch a new wpf project, maybe you broke something and i guess you did not go very far yet ;-) – GameAlchemist Oct 06 '12 at 14:41
  • 1
    I agree with @VincentPiel, you shouldn't need to manually add references to standard assemblies, start with a new project. – Surfbutler Oct 06 '12 at 15:58

1 Answers1

0

I found the missing assembly WindowsBase.dll! It was buried and I had to browse for it!

Now, I've only got one error left... myGrid does not exist in the current context.

Is this declared in the XAML window? I suspect that it is. But that is in another project. How do I reference it the MainWindow across two projects that exist in the same solution?

zetar
  • 1,225
  • 2
  • 20
  • 45
  • Yes, looking at the example, myGrid would be any grid (or canvas) that your form consists of, nothing special. The xaml would not be in another project, you would have your xaml file called something.xaml and the c# code would be in a something.xaml.cs file in the same project. Just name the grid myGrid (Name="myGrid") and the C# will compile ok. – Surfbutler Oct 06 '12 at 15:53
  • I'm not in a position to start a new project; I have to add my code to an existing project. One last question: so myGrid is part of MainWindow.xaml... but I need to reference myGrid in another project (that is part of the same solution)... How do I do this? – zetar Oct 06 '12 at 18:57
  • That is to say, how do I reference MainWindow.xaml in another C# file in another project that's part of the same solution? – zetar Oct 06 '12 at 19:05
  • myGrid needs to be in a project that is a ClassLibrary. Then in your other project you add a reference to it, and if the namespaces are different, add a using statement. – Surfbutler Oct 06 '12 at 19:11
  • If I change the Output type of Project1 (where MainWindow.xaml is located) to Class Library it throws the error "InitializeComponent' does not exist in the current context. Isn't there a way to reference myGrid in MainWindow.xaml from Project2 that is part of the same solution? – zetar Oct 06 '12 at 19:17
  • Project2's Output type is a Class Library. – zetar Oct 06 '12 at 19:19
  • You can only reference class libraries (which are basically DLLs), not executables. Not sure why you get the error changing the type. Try creating a new class library with the create new project wizard and add your control to it. – Surfbutler Oct 06 '12 at 19:29