c# GDI+ rendering problem - c#

Not sure if anyone else has seen this, but I am rendering a custom control that has zoom functionality. Lets say for simplicity sake that I am just drawing one rectangle with border width = 1 in the viewable area of the control. When I alter the zoom (graphics.ScaleTransform()) the rectangle's borders take on different sizes (sometimes the same, mostly different). This makes my control look ugly when the user zooms in or out. I'm sure it's something really simple but i'm struggling to fix it. Any help appreciated!
thanks,
Thornza

Yes, the pen width gets scaled as well. Create a pen with a Width = 0. That will always be one pixel wide, regardless of the ScaleTransform.

Related

Dynamical rectangle plot c# wpf

I want to create a plot that dynamically displays active elements as rectangles. I have achieved a first version that is actually ok using OxyPlot.Annotations.RectangleAnnotation which I add to myPlotModel.Annotations, you can see it in the image hereafter:
Example of wanted display
The thing is that after a while, the amount of drawn rectangles make the update not smooth as I update the shown timewindow (which is set to 15 seconds). I have already set a maximum of drawn elements that suffice to cover the displayed window (i.e. the rectangles get removed as they are too far in the past), but the rendering is still jerky. I draw the rectangles by allocating them to an equal fraction of the Y-axis, that is the third one from the top gets:
rowNumber= 3.0
minimumY = maximalY - maximalY / totalElements * rowNumber
maximumY = maximalY - maximalY / totalElements * (rowNumber + 1.0)
And the Y-axis is hidden.
My question:
Is there a smarter way of creating such a display that would be less computationally heavy, and therefore allow a smoother update? I do not have to stick to OxyPlot, it is simply the easiest way that I found to obtain what I wanted.
Thanks for your answers!
Technically, the answer to your question is "Yes".
There are a number of ways to do this.
You could have a vertical itemscontrol that had an itemscontrol in it's template. That could have a canvas as it's itemspresenter and you could bind canvas.top and canvas.left to properties in it's content. Template each into a rectangle and bind height and width.
And of course do something about the scale on the bottom and the column of activity labels or whatever you want to call them there.
Unless you're using an absolutely ancient machine, that'd just fly.
It's quite a lot of work but it would probably be quicker to write that than to search through a load of alternative packages and decide which was optimal.

How to move a rectangle that's used as a Meter bar in unity

I want to make a vertical meter for values between 2 ranges.
so what i tried so far, was to make a frame, and under it put a mask, and under that a rectangle.
with the idea, of lowering/increasing the rectangle Y position to make it seems to move.
rect.position = new Vector3(rect.position.x, (rect.position.y - rect.sizeDelta.y));
so i did that, to first lower by it's height, so the bar will seem empty.
but it doesn't work right, the position depends on the screen size, and the rect Y end up way lower then exactly his height.
so, tl;dr, i want a bar that i can give a value between 0-100 and it will fill it according to that.
and i might have gone wrong about it.
so, any suggestions would be appreciated.
edit:
Add picture to show what i want the bar to look like:
Well i feel kinda dumb
I didn't realize how customizable the Slider control is.
I removed it's handle, unchecked interactble and removed it's background, and now i can give it a value between 0-100 and it works exactly as i wanted.
thanks Mathias Siig Nørregaard for referencing me to the video link that opened my eyes.

How do I get the dimensions of the ImageRectangle in PictureBox?

Background
I want to be able to get the drawn dimensions of a zoomed image inside the picturebox (I'll explain below).
The PictureBox.ImageRectangle property seems to be exactly what I'm looking for because it shows the height and width of the resized image, as well as it's relative top, left position inside the control.
The trouble is PictureBox.ImageRectangle is private and so I can't read the values without using reflection (which is obviously not ideal).
Actual Question
My question is, is there another way that I can easily get to these values without writing a method to calculate what the values "ought" to be? I can do that easily enough, but I feel I'd be reinventing the wheel.
Context:
I'm writing a simple image processing app in C# and one of the things it has to do is allow the user to draw a selection around a portion of the image (a lot like the Marquee tool in Photoshop).
I need to know the dimensions of the rendered image so I know where to set the bounds of my marquee tool and also to translate the values of the drawn rectangle to points on the scaled bitmap inside the control.
My answer look simple so maybe I'm missing something, but I think Control.DisplayRectangle suits your need.
EDIT
OK, missed the point; however see How to get the value of non- public members of picturebox?
if you want to access dimension of the image in picture box you can use
GraphicsUnit units = GraphicsUnit.Point;
RectangleF imgRectangleF = pictureBox1.Image.GetBounds(ref units);
Rectangle imgRectangle = Rectangle.Round(imgRectangleF);

Image rescale (downsize) with Silverlight

I have an array of graphs, that are 2000x1000 pixels pngs. When I put them into a silverlight Image that auto-sized with the browser window, DisplayImage.Source = new BitmapImage(GetHeatmapURL()); they images look distorted and for some browser sizes much worse than for some others.
I want to fiddle with some setting that would improve the quality of this downsampling, is this possible? Am I doing something wrong? My first approach was to put the Image into a Viewbox. That looked even worse. Googling gives virtually nothing useful...
Any help much appreciated.
PS. I'm working with Silverlight 4.
You could check out the WriteableBitmapEx project on CodePlex which gives you a Resize() extension method for the Silverlight WritableBitmap class where you can use either Bilinear or Nearest Neighbor interpolation. Resizing with the Bilinear interpolation might give you something that looks better than the ViewBox resizing, but you would have to test it out.
var resized = writeableBmp.Resize(200, 300, WriteableBitmapExtensions.Interpolation.Bilinear);
When making use of the Viewbox you need to make sure to set Stretch to Uniform to respect the original height/width ratio.
In regards to the rendering of the image at the given aspect ratio and size; that is contingent on the framework.

WPF position and dimensions wrong

I'm currently using a semi-transparent WPF form with no border as a camera style device in a program of mine. However, when I access the position (top, left) and dimension (height, width) properties of the form, it would appear they are wrong.
The top property reported to be roughly 26 pixels higher than it actually is in relation to the desktop. IE if I put the forms top and left at (0,0) the properties will report (0, -26).
The height is also incorrect, reporting about 50-60 pixels shorter than it actually is.
Has anybody experienced this problem in the past and have a solution?
Regards,
Andy Hunt
if I understand correctly, this is no problem at all but the expected behavior.
WPF uses DPI-independent virtual units for measuring size and position, not actual pixels.
There are many sources like this explaining this.
If I understood the question wrong (its late and I am tired ;) could you provide a code sample illustrating the issue?
Andrej

Categories