I'm trying to set the height of a WPF element using C# to a programatically calculated value.
I've already tried the obvious of setting the height to a variable, a constant variable and just straight-up writing the calculation during the assignment.
Image image1 = new Image()
{
Stretch = Stretch.UniformToFill,
Height = (450 / 650) * 260
};
I'm expecting that the image appears with the size 260x180, but the image doesn't appear at all. Setting the Height to be 180 (the outcome of the calculation) makes it look as expected, though.
How do I fix it and why does it happen?
You need to convert your calculations not to use integers
Image image1 = new Image()
{
Stretch = Stretch.UniformToFill,
Height = ((double)450 / (double)650) * (double)260
};
Didn't test it, but something like this should do the trick.
The problem is that 450 / 650 is 0 when using int.
Maybe 260 * 450 / 650 works too.
Related
I've a little issue over here. I'm resizing the image using the following code snippet:
public static BitmapFrame FastResize(BitmapFrame image, int width, int height) {
var scaleX = width / image.Width * 96 / image.DpiX;
var scaleY = height / image.Height * 96 / image.DpiY;
var target = new TransformedBitmap(image, new ScaleTransform(scaleX, scaleY, 0, 0));
return BitmapFrame.Create(target);
}
The problem is when I'm calculating the value of scaleX and scaleY. Some of the image has DPI, but some of them has 0.00 or I can assume the DPI is not set for those images. And as a result, I'm having value Infinity for those two scaleX and scaleY variables. Because the value of image.DpiX and image.DpiY are 0.00. Well, it is not throwing Attempt to divide by zero exception.
In order to achieve a high and expected quality after the image scaling/resizing process, I need to calculate the scaleX and scaleY in that way: width / image.Width * 96 / image.DpiX. However, as I've already mentioned, some of the images doesn't have DPI set.
So my question is what should I do in this case if the image has no DPI set, in order to prevent having a value Infinity for those variables? Is there a default DPI value we can use for images if it is not set? Or may be a way of calculating PDI programmatically using WPF/WIC (WPF and Windows Imaging Component) in case even if it is not set?
Check if all your images are pixel based. Vector based images don't have DPI.
I want to be able to set the number of lines in a multilined TextBox.
I've tried the following:
int initHeight = textBox1.Height;
textBox1.Height = initHeight * numOfLines;
But this makes it too large when numOfLines gets large. So then I tried this:
float fontHeight = textBox1.CreateGraphics().MeasureString("W", textBox1.Font).Height;
textBox1.Height = fontHeight * numOfLines;
But this was too small when numOfLines was small, and too large when numOfLines was large.
So I'm doing SOMETHING wrong... any ideas?
This would set the exact Width & Height of your multi line Textbox:
Size size = TextRenderer.MeasureText(textBox1.Text, textBox1.Font);
textBox1.Width = size.Width;
textBox1.Height = size.Height + Convert.ToInt32(textBox1.Font.Size);
Something like this should work:
Size size = TextRenderer.MeasureText(textBox1.Text, textBox1.Font);
textBox1.Width = size.Width;
textBox1.Height = size.Height;
This was from C# Resize textbox to fit content
What you are doing should work, but you need to set the MinimumSize and MaximumSize I am not 100% positive, but I think this constraint will still hold if height is set via code
From the documentation of Graphics.MeasureString:
To obtain metrics suitable for adjacent strings in layout (for example, when implementing formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that takes a StringFormat, and pass GenericTypographic. Also, ensure the TextRenderingHint for the Graphics is AntiAlias.
As such, you should use one of these overloads, such as this one, which allow you to specify StringFormat.GenericTypograpic to get the required size.
Try this:
float fontHeight;
using (var g = textBox1.CreateGraphics())
fontHeight = g.MeasureString("W", textBox1.Font, new PointF(), StringFormat.GenericTypograpic).Height;
I am trying to set DocContent DockLeft to custom size but it is not working. Can anyone suggest how to accomplish this?
I was testing this code but it ends up with default width 404 not 700 as I set.
dockPanelMain.SuspendLayout(true);
DockContent myContent = new MenuForm();
myContent.ShowHint = WeifenLuo.WinFormsUI.Docking.DockState.DockLeft;
myContent.Show(dockPanelMain);
dockPanelMain.DockWindows[WeifenLuo.WinFormsUI.Docking.DockState.DockLeft].Width = 700;
dockPanelMain.ResumeLayout(true, true);
DockLeftPortion
Size of the left docking window. Value < 1 to specify the size in portion;
value > 1 to specify the size in pixels.
I am looking for An ImageResizer Like below that supports MaxWidth & MaxHeight ...
where can i find it?
the below module does many other jobs that are not necessary for me.
just want to change format & support maxwidth and maxheight.
ImageResizer
You can write a wrapper that enforces the maximum width and maximum height, and maintains the aspect ratio.
For example, say you have an image that's 640 x 120 and your maximums are 1,920 x 1,440. Now, you want to make that image as large as possible, so you write:
ResizeImage(image, 1920, 1440)
If you were to do that, the aspect ratio would be shot.
You need to compute the aspect ratio of the existing image and adjust the values.
// Compute existing aspect ratio
double aspectRatio = (double)image.Width / image.Height;
// Clip the desired values to the maximums
desiredHeight = Math.Min(desiredHeight, MaxHeight);
desiredWidth = Math.Min(desiredWidth, MaxWidth);
// This is the aspect ratio if you used the desired values.
double newAspect = (double)desiredWidth / desiredHeight;
if (newAspect > aspectRatio)
{
// The new aspect ratio would make the image too tall.
// Need to adjust the height.
desiredHeight = (int)(desiredWidth / aspectRatio);
}
else if (newAspect < aspectRatio)
{
// The new aspect ratio would make the image too wide.
// Need to adjust the width.
desiredWidth = (int)(desiredHeight * aspectRatio);
}
// You can now resize the image using desiredWidth and desiredHeight
It doesn't matter if the library does more than what you need. If it does what you need it to, use it. The extra stuff isn't going to impair you at all.
Here's the problem. I have a picture that will have a different Height every time according to the picture that's loaded.
I'd like to scale that picture so it fits inside of the document which has 800 pixels height. So if the image has 2560px in height, I need to find out the PERCENTAGE needed to bring that pixel height down to 750 so it fits snuggly on the page.
I'm stuck trying to find the formula for this simple enough problem.
Here's my code:
iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(
image, System.Drawing.Imaging.ImageFormat.Jpeg);
if (pic.Height > pic.Width)
{
//Maximum height is 800 pixels.
pic.Height formula goes here....
}
else
{
//Maximum width is 600 pixels.
pic.Width formula goes here....
}
Some number p is such that p * 2560 = 750. Therefore, p = 750 / 2560 = 0.29296875.
Of course, make sure that you do floating-point division.
The rule of three will help you sort it out.
I don't know if I understand your problem exactly. Do you mean something like this?
percentage = (frameHeight / picHeight) * 100
Example:
(750 / 2560) * 100 = 29
That means: 2560 * 0.29 = 750
Here x is the maximum desired height, y is the actual image height and p is the percentage.
p = x / y;
x = p * y;
y = x / p;
Given any two you can find the other.