Setting Image Width/Height in codebehind - ASP.NET, C# - c#

I am trying to set the width and height of images in a datalist in the codebehind.
The plan is to do something more complex than this based on the width/height, so setting width and height in the aspx file to 50% is not an option.
For some reason I always get 0 for width and height. Image1.ImageUrl is what i would expect though. Any ideas? Image is the System.Web.UI.Webcontrols.Image, not a System.Drawing.Image.
protected void DataList9_ItemDataBound(object sender, DataListItemEventArgs e)
{
Image Image1 = (Image)e.Item.FindControl("Image1");
double height = Image1.Height.Value;
double width = Image1.Width.Value;
height = height * 0.5;
width = width * 0.5;
Image1.Height = new Unit(height);
Image1.Width = new Unit(width);
}

Your code above is referencing a image control that more then likely does not specify a width and or height.
To do what you want I believe you would need to get the source of the image and load it in memory using GDI. Then you could determine the width and height. Then you would be able to do your adjustments to the height and width and apply to the properties of the image tag.

Remember with an img tag, you don't set width and height by default. In this case, unless you set Width and Height, they will be 0 (or undefined). If you need the actual (image, pixels) width and height, you'll need to discover that for yourself by loading the image into memory.
Depending on the filetype, .NET probably has a decoder or the ability to load it already. Load it into a Bitmap then query the width and height there.

I am not sure about c# but I can code it this way in VB.net, so I am sure there is something equivelant and it looks like it will solve your intent if not your actual coding problem.
With dImage
.Width = 50%
.Height = 50%
End With
I tried it both with setting the image height in my xaml as well as leaving it out.
--edited for layout purposes.

Have you tried declaring/defining an OnLoad handler for the image and setting the height/width in there?

Related

Why after applying Canvas.Left to my Image it goes out from the canvas? (In WPF)

my canvas is 1920*1080 and I'm adding an image that its width is 200 and height is 200 too,
I want the image to go to the top right corner, and after setting the following code and running the application
I see that the image overflows the canvas:
Image img = new Image();
img.Source = SOURCE;
img.Width = 200;
img.Height = 200;
screen.Canvas.Children.Add(img); // I have already created screen obj and a canvas before
img.SetValue(Canvas.LeftProperty, 1720);
img.SetValue(Canvas.TopProperty, 0);
after I checked it manually, I saw that if I put the Canvas.Left 1350 it fit the target,
but I can't understand why it happens
...................
And about the canvas parent:
it's in the window and I maximize the window at startup also I'm using the code below in its window constructor to maximize the canvas too:
var resolution = Screen.PrimaryScreen.Bounds;
Canvas.Width = resolution.Width; // i put the name of the canvas, Canvas :)
Canvas.Height = resolution.Height;
.................
another update:
I find out however I get the screen size 1920x1080 but the real size is 1536x864 so can anyone help how to get the real screen size? :)
So the problem was I wasn't setting the actual width and height for my canvas,
when I use the resolution width and height, what I get is 1920x1080 but also I checked in my display setting that I have 125% scale so it makes the size 1536x864 so I got this number with RenderSize:
screen.Canvas.Width = screen.RenderSize.Width;
screen.Canvas.Height = screen.RenderSize.Height;
And also it's worth mentioning that if you use it in the constructor the RenderSize will be 0
Why my resolution of Thinkpad T450s is 1536px width?

How to get the correct Image height and width

I am trying to calculate the size of a Rectangle using the Height and Width of an image in my project. The image (icon) is 20px W and 16px H. When I load it in from a ImageList and look at the image size, it shows that it is 16px X 16px.
I assume that there is some compression that is being applied to the image, but I am not very experienced in this area. I just need to get the correct, dimensions.
Is there any way?
NOTE: I am not loading the file from disk... it is in my project.
This, simply, is what I am doing:
var tabPage = _tabControl.TabPages[e.Index];
var image = _tabControlImageList.Images[tabPage.ImageIndex];
var imageHW = Math.Max(image.Height, image.Width);
You can and must declare a ImageSize you want to have, before you load the Images into the ImageList:
_tabControlImageList.ImageSize = new System.Drawing.Size(20, 16);
Afterwards all your Images inside the ImageList will have following size: 20x16px.

Scale image to fit to A4 page - Migradoc

I am really struggling to get this right, any help would be appreciated.
I have a series of images that I want to build in to a PDF using MigraDoc (1 image = 1 page)
Each image must be displayed on a separate page but may not extend over the page it must fit on to the page perfectly.
So, how do I scale an image (of any size) to fit to a page using MigraDoc?
You call AddImage() to add the image - and in return you get an Image object that allows you to set width and/or height of the image.
What you have to do: check the dimensions of the image, calculate which is the limiting factor (width or height), then set this limiting factor on the Image object and also set LockAspectRatio.
Or set both Width and Height and leave LockAspectRatio off.
For DIN A4, you may allow e.g. 19 cm x 27.7 cm as maximum image size.
For an image with 1000x1000 pixel you would set the width to 19 cm (assuming LockAspectRatio is on). Height will then also be 19 cm automatically.
For an image with 1000x2000 pixel you would set the height to 27.7 cm. Width will then be 50% of the height.

Why Image size is NaN (wpf) and how to resize via mouse wheel?

I want to load an image and put it into a viewbox. The image is displayed correctly, but, when I'm going to get it's width and height, it's both NaN.
This is my image load code :
Image img = new Image();
img.Source = (ImageSource)new ImageSourceConverter().ConvertFromString("1.png");
Viewbox vb = new Viewbox();
vb.Width = img.Width;
vb.Height = img.Height;
vb.Child = img;
cnv.Children.Add(vb);
The reason I want to get the image width and height is so I could resize it (via viewbox resize) later inside the application.
Any idea how to get the image dimension?
And this is how I'm going to resize it (via mouse wheel)
private void cnv_MouseWheel(object sender, MouseWheelEventArgs e)
{
if (vb != null)
{
vb.Width += Mouse.MouseWheelDeltaForOneLine;
vb.Height += Mouse.MouseWheelDeltaForOneLine;
}
}
And it returns error and said that vb.Width is not a valid value.
Questions to sum this up :
How to get the image width / height in wpf?
How to resize the viewbox (which will also result in image resize) via mouse wheel? For example, if I scroll up the wheel, the width and height is added by 1, and if I scroll down the wheel, the width and height is reduced by 1.
Thank you
P.S. Viewbox vb; and Image img; is a global variable, I just shorten the script down.
P.S.S. I know if the Viewbox width and height initialized by a number, lets say 100 and 100, it will work, I just wanna know how to get the image original size.
EDIT :
The resize can be achieved by detecting whether it's scrolled up or down by detecting e.Delta > 0 or e.Delta < 0
(Source : http://social.msdn.microsoft.com/Forums/vstudio/en-US/170c4fd0-1441-4d83-903d-594af75f8fb4/detect-mouse-scroll)
It seems as though the Image object is not fully loaded at that stage. I believe that you can use the Width and Height properties of the ImageSource class instead. That should be fully loaded at this stage.
ImageSource.Width Property
ImageSource.Height Property
For other users, you can also possibly find the values that you want from the ActualWidth and ActualHeight properties of the Image class (inherited from the FrameworkElement class) instead of the more usual Width and Height properties.
FrameworkElement.ActualHeight Property
FrameworkElement.ActualWidth Property
The image original size can only be obtained in Pixels while your WPF controls are measured in Device Independent Pixels so you're going to have to make a translation from Pixels to Device Independent Pixels and back wherever necessary.
That said, to obtain the width and height for a png file, you can load it into a BitmapImage instead of Image and query the PixelWidth and PixelHeight properties for the BitmapImage.
Again, be aware that this is the only place in your code where you're going to be dealing with Pixels, all Width and Height properties (including ActualWidth and ActualHeight, only set after the control is fully rendered) are measured in Device Independent Pixels. This can be misleading because in a lot of situations this happens to be a 1:1 conversion but this is not guaranteed to be the case.
More info can be found here: http://msdn.microsoft.com/en-us/library/windows/desktop/ff684173%28v=vs.85%29.aspx

.net Resizing control won't work?

I have a couple of pictureboxes that need to be resized by aspect ratio when the window size changes. I assumed I can anchor the width, but set the height manually (i.e. anchor the left, right, and top edges; but not the bottom.) However, my control won't resize if I try changing the Size property. Why wouldn't that work? How can I resize my control?
private void Form1_Resize(object sender, System.EventArgs e)
{
int oldWidth = 1280;
int oldHeight = 1024;
int newWidth = webcamPictureBox.Size.Width; // this is auto-resized w/ window; becomes 591
int newHeight = (oldHeight * newWidth) / oldWidth; // this is calculated manually; becomes 472
// Control won't resize if I change the Size property
// Size property stays the same
this.webcamPictureBox.Size = new Size(newWidth, newHeight);
this.thumbnailPictureBox.Size = new Size(newWidth, newHeight);
}
You could be running into one of a few problems, I suppose:
What's the SizeMode of the PictureBox? Is it AutoSize, StretchImage, or something like that? It should probably be Normal for what you want to do.
Are you sure you have your anchoring set up correctly?
Did you debug and check the final size of the picture boxes you're trying to resize? You should verify that it's what you expect and that the form has been repainted.
These are just some ideas; your code along is not enough to provide a great response.
It's difficult to answer this question definitively with the information you've posted.
Windows forms controls problems are difficult to debug by nature because of all the controls that might be affecting what you're doing. First off, you'll want to try debugging this on your own on a form with as few controls as possible. Are there any circumstances under which the resizing behaves properly?
That said, is the Dock property set on these controls? They definitely won't resize if they're set to DockStyle.Fill.
Thanks for the ideas -- they pointed me on the right track! Everything was set up as they should be, except the TableLayoutPanel they were in was constraining their sizes. I determined this by noticing the pictureboxes' size values were simply uneditable, both in runtime and in the designer.
So I set TableLayoutPanel's AutoSize to true, and it works great!

Categories