VS Express 2015 C#: picturebox width/height not in pixels? - c#

If I create picturebox element on a form, the properties panel allows me to set the width/height to a value representing pixels.
However in code it seems a different unit size is used for measurement.
Example; i put the width at 128.
Both in the editor and in runtime the form will show a picturebox of 128 pixel width.
Now, If i try to read picturebox.width value in code it gives back 85.
If I try to change the width to 128 in code, it gives me an actual bigger box then 128 pixels.
What am I missing here?

Related

WinForms does not scale sizes correctly when changing DPI (even for simple controls)

I cannot get WinForms to properly scale the UI when the DPI setting is changed: different controls are scaled by a different factor, messing up the UI design.
This is what I’ve tried:
In Windows 7 set the text size to 100% (= 96 dpi).
Start Visual Studio 16.9.1 and create a new WinForms app with C# and .NET 5.0
Add a simple text box and a button to the form
Compile the project
Run the program: both controls have a height of 23 pixels (checked visually and programmatically)
Set the OS text size to 150% (= 144 dpi) and log out and in again to apply the changes
Run the program again: now the text box has a height of 31 pixels and the button has a height of 38 pixels.
So when increasing the OS text size (= DPI) by 50%:
the height of the text box increases by 33%
the height of the button increases by 67%
Notes:
I checked all guidelines described here: How to write WinForms code that auto-scales to system font and dpi settings?. The application is DPI-aware and AutoScaleMore and AutoScaleDimensions are set correctly (default values in .NET 5.0).
I also tried this with .NET 4.7.2 and Windowds 10, but the two controls always scaled differently.
Why are these controls scaled differently?
Why are they not scaled by 50% as you would expect?
What can I do to have these two controls scaled by the same factor?

RichTextBox space issue

I created an extended RichTextBox with better image displaying support. For short: I parse text-based image placeholders from the RTF-input, replace them by an empty paragraph with a propriate spacing (image height) and draw the images in the paint event above the text (inside the spaces).
The problem now is that the spacing seems to be wrong. I calculated the twips with the following formula:
size.Width = (int)((1440 * size.Width) / graphics.DpiX);
size.Height = (int)((1440 * size.Height) / graphics.DpiY);
Where graphics is from my RichTextBox and size is the image size. As the DPI value is 96, it is basically twips = 15 * pixels.
I tried the RTF control words \sbN and \saN with my calculated twips-value (I controlled it with the debugger, the value is as expected). I also used interop with PFM_SPACEBEFORE and PFM_SPACEAFTER.
Both ways give the same result. The problem is that the real space inside the RichTextBox is too big. If I multiply the calculated twips value with 0.75 it fits. But I really don't get why this happens.
My first thought was the factor 72 / 96 (PPI / DPI) which is 0.75. But this makes no sense for me.
The additional space increases proportional to the image height. So the space is barely noteable for small icons (e.g. 24 pixels height). But for larger images (e.g. 320 pixels height) the additional space is huge.
Some ideas? Is this a bug in the RichTextBox control?
To clarify: I used System.Windows.Forms.RichTextBox as a base class.
Ok I found the error by myself. I had to use the DPI values of the image instead of the DPI values of the control. The image DPI values were 120 so the factor was 0.8 (not 0.75). Now it fits. I leave the question here in case someone has the same problem.

How to get blank space size when using PictureBoxSizeMode.Zoom of PictureBox

I am using PictureBox class in Windows Forms. Also I set PictureBox's SizeMode to PictureBoxSizeMode.Zoom.
How to calculate this black width what is added to the picture? Or how to get ratio what is used to scale this picture? Is there some existing property to get real size of image?
Is there some existing property to get real size of image?
PictureBox.Image.Size
How to calculate this black width what is added to the picture? Or how to get ratio what is used to scale this picture?
There is no standard way of doing that. But you can look at http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/PictureBox.cs,5c2ab37313f547c2 how PictureBox does that internally.

Autoscroll not working when child control exceeds 32767 width

I am using a panel and auto scroll for printing texts in bitmap Editor(Bitmap Editor - a control created by me for printing texts in ellipses). My issue is that the texts are simply ignored or is not showing in the bitmap editor when the width of bitmap editor exceeds 32767 size.
Is that possible to use auto scroll other than in panels or please anyone help me to solve this.
Thanks a lot for helping me on this....
At lasts I got the reason for the error mentioned above.
LPARAM - A Data type of Windows used to pass message parameters to Windows Procedure.
It is a 32 bit pointer which passes message as two parts
i.e in High order(First 16 bits of 32 bits) and
Low order(Second 16 bits of 32 bits).
Where
High order is used to pass the height of the control and
Low order is used to pass the width of the control
So if the height or width of the control exceeds 32762size, it shows error
because
32767 is the highest number that can be represented in a signed 16bit integer.

How can I check the dimensions of a image so that I can reduce them proportionaly to avoid distortion?

When people write an article, they submit a photo to illustrate the event. But, the space to display is not wide. So, I'd like to reduce they width and/or height while keeping their original proportion. Otherwise, the image gets distorted.
Let's say the max width is 300px. Anyting wider than that would see its width reduced to 300px. However, I'd like the Height to be reduced to the same proportion. For instance: 600 x 800 will become 300px x 400px. So, I need to be able to check the dimensions
How do I check those dimensions?
When do I check them? (i) after uploading, (ii) while retreiving from the database?...
Thanks for helping
I would suggest you check the image after upload and save two versions to the database - the original and the resized version. You should serve the resized versions with the article. That way you resize the image only once.
For code example take a look here:
C#: Resize An Image While Maintaining Aspect Ratio and Maximum Height

Categories

Resources