How to get the correct Image height and width - c#

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.

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?

c# use emgu imagebox control to display a image

I use the imagebox to display a image(imgWidth:19200, imgHeight:260),and my code is:
imageBox = new ImageBox();
imageBox.Size = new Size(imgWidth, imgHeight);
imageBox.Dock = DockStyle.Fill;
MainSplitContainer.Panel1.Controls.Add(imageBox);
imageBox.Image = new Image<Bgr, byte>("2.bmp");
But the displayed image width is only about 15000, and the right part of the image can not be displayed. How can I display the full image?
It is possible that an ImageBox won't do more than 15,000 pixels in width. However, have you tried setting the functional mode to Pan and Zoom? Maybe that will get you there.
Doug

Getting "image" coordinates of picturebox in zoom mode

I am using a zoomed picturebox. I want to retrieve the image Top-Left and Bottom-Right coordinates. But it is different from that of picturebox when the aspect ratio of the image doesn't match the picturebox. I wonder how I can get the image coordinates on the form.
Minus the Image size divided by 2 from the PictureBoxsize plus the Image size again.
This uses the Size.Subtract Method (Size, Size). MSDN
Size sizestep1 = Size.Subtract(new Size(PictureBox1.Image.Size.Width / 2, PictureBox1.Image.Size.Height / 2), PictureBox1.Size);
Size finalsize = Size.Add(sizestep1, PictureBox1.Image.Size);
// Convert to point.
Point BottomRightCoords = new Point(finalsize.Width, finalsize.Height);
And if you want to get the BottomRightCoords on the form, you have to add the PictureBox Location to it.
A little bit of mathematics suggested above + the code in the following link did the trick:
How to retrieve zoom factor of a WinForms PictureBox?

Blur images shown in listview

In my winform app, i am adding images to listview using ImageList.
but when i increase the Image size (i.e. height & width) small images(ex. bmp images) got blurred.
is there any way to prevent imgs from blurring ??
Thanks in advance.
In .NET ListView, you probably have to wrap the image in standard-sized bitmap and then put the whole bitmap in the item:
Bitmap bmp = new Bitmap(64, 64);
Graphics grfx = Graphics.FromImage(bmp);
grfx.DrawImage(
myImage,
(bmp.Width - myImage.Width) / 2,
(bmp.Height - myImage.Height) / 2);
listView.Items[0].Image = bmp;
You may try Better ListView Express or Better ListView. It supports images of arbitrary sizes out of the box. When the image is small, it puts it on center without resizing and prevents the blurring:
enter link description here
If those are small images then obviously it will be blurred. If you want bigger images and you cant find them on Internet then try to create your own images by outlining the small images in photoshop or gimp(free product).
No there isn't. If you're making images bigger than their original size they always lose quality. Try using bigger images at first.

Setting Image Width/Height in codebehind - ASP.NET, 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?

Categories