I am trying to emulate a POS printer with System.Drawing and one of the functions I need is to draw text at double height. Any idea how I can do this using .Net's Graphics class?
Do I need to draw the text twice as large and condense it or draw normal size and then stretch? Both seem like messy options but is there an alternative?
Look at the transformation matrix on the Graphics object - you can control horizontal and vertical scaling independently.
Use a ScaleTransform and only scale up the y.
Related
I'm using monogame. I want to overlay Text onto one of my button images, but I want to use the image way of drawing;
spriteBatch.Draw(texture, destinationRectangle, sourceRectangle, Color.White);
so that I can control how the text is handled inside the bounds of the button image rectangle, like trim excess.
Is there a way to draw strings using the fontsprite, without using the spriteBatch.DrawString method, or is there more to this method that I haven't learned yet? It just seems limiting from its input parameters.
You can use Font.MeasureString to get the width and height of a string as a Vector2. You can subtract half of it from the center position of a Rectangle and you get your centered text.
I created simple form and now I want to draw single objects (rectangle, circle, lines..) in relative coordinates.
Main problem for me here is to create 2D cartesian coordinate system in middle of the form.Is it possible and how to do it??
Main question is how to efficiently transform absolute coordinates to relative? How to create my own system so I get results in numbers (negative, positive depending on quadrant) and not in pixel?
.
Currently I made MouseMove event to display current location of mouse with Cursor.Position.X, Cursor.Position.Y and display it in label. Displayed coordinates are pixels, how to change that? I've read something about converting with PointToClient method but I don't really understand it.
I am tied to windows forms becouse I already made a program in win forms and now I just want to add this feature to it.
Thanks
What you're most likely looking for are Graphics.TranslateTransform and Graphics.ScaleTransform.
private void Transform(PaintEventArgs e)
{
e.Graphics.ScaleTransform(width, height);
e.Graphics.TranslateTransform(xOffset, yOffset);
}
You'll also need to make sure that whatever drawing method you're using has an overload for either PointF structures or Singles.
Of course, you could also just handle all of this on your end. Scaling your input/output coordinates by the width and height of the form and then translating by half of that will give you the same results.
Edit- To add some clarity, Windows always expects your coordinates to be in pixel form. You can use the above transformations to do your work at a different scale and then transform into the form that Windows expects.
Setting the translation should be straightforward. If you want the middle of the form, you simply want to translate by half the width and half the height. How you choose to scale your coordinates is going to depend on the range that you need to work in (ie, -1.0 to 1.0, -0.5 to 0.5, etc.).
Is there a way to stretch the X or Y axis when calling System.Graphics.DrawString? We are working on laying out a graphics image (which is actually going to be printed) where it would be nice to make a particular piece of text say 125% higher than it's normal height (while maintaining the same width). Is there a best way to do this?
Sure, Graphics.ScaleTransform() works fine as long as you use Graphics.DrawString() instead of TextRenderer. Keep it modest, stretched text doesn't look that good. 125% ought to be okayish.
I'm working on a graphical program and I'd like to get corner points from figures I mentioned in the title. I need them because at those points places, I want to place white, little rectangles that allows me to resize those figures.
If you place all your shapes in a Canvas you should be able to get the x and y axis using the GetLeft and GetTop methods of the canvas class. From there you can use the shape's width and height to place your white little rectangles.
I have an image that is 1px wide, and some height. I need to draw this image across the entire width of the control on it's OnPaint event. I get it to draw, however not correctly. It seems like when it stretches it, it doesn't actually fill all the pixels. As if the interpolation is off. Is there a way to say "stop being smart, just draw it already"? I see no InterpolationMode.Off or .None in the options for the graphics object.
I can confirm I actually drawing the full width by using an image of width X where X is the same width as the control. Then when it draws, it covers the full area as normal. However this control is resized all the time, and to save memory and all that jazz using 1px wide images is quite normal in the web world. This is for a desktop C# application though. Any ideas on how to fix this?
Ok I figured out the magic keywords:
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
This coupled with setting the Interpolation Mode to NearestNeighbour allows for a full block to be drawn.
Without setting the Interpolation mode, you get weird blending (expected).
Without setting the PixelOffsetMode, the nearest neighbour algorithm has no neighbour to compare to on a blank paint and therefore only draws half the image, for half the width. Setting it to offset half, moves everything over by -0.5px, and allows this algorithm to work for block textures.
InterpolationMode.NearestNeighbor is what you want to use in this case.