C
C#16mo ago
Lep

❔ Auto Resize

I'm attempting a little learning project and was wondering if I could get some help on the following:
var newimg = new BitmapImage();
newimg.BeginInit();
newimg.UriSource = new Uri("Images\\Snoop.gif", UriKind.Relative);
newimg.EndInit();
ImageBehavior.SetAnimatedSource(img, newimg);

MainWin.Width = img resized width; <-----

SizeChanged += (o, e) =>
{
var r = SystemParameters.WorkArea;
Left = r.Right - ActualWidth;
Top = r.Bottom - ActualHeight;
};
var newimg = new BitmapImage();
newimg.BeginInit();
newimg.UriSource = new Uri("Images\\Snoop.gif", UriKind.Relative);
newimg.EndInit();
ImageBehavior.SetAnimatedSource(img, newimg);

MainWin.Width = img resized width; <-----

SizeChanged += (o, e) =>
{
var r = SystemParameters.WorkArea;
Left = r.Right - ActualWidth;
Top = r.Bottom - ActualHeight;
};
How can I get the automatically resized width of the new img? My current main window has a capped height of 300. The width is auto and the img has an auto height and width so it scales to the 300 pixel height. If I manually change the main window width size everything works perfectly fine. I tried the following but it didn't seem to work: MainWin.Width = (MainWin.Height / img.ActualHeight) * img.ActualWidth; which would give me 146.25 to be the correct width. It works if I insert the size manually, but it doesn't work if I use the equation to set the size. I've also tried:
SizeChanged += (o, e) =>
{
var r = SystemParameters.WorkArea;
//Left = r.Right - (234 * 300 / 480);
Left = r.Right - (img.ActualWidth * MainWin.Height / img.ActualHeight);
Top = r.Bottom - ActualHeight;
};
SizeChanged += (o, e) =>
{
var r = SystemParameters.WorkArea;
//Left = r.Right - (234 * 300 / 480);
Left = r.Right - (img.ActualWidth * MainWin.Height / img.ActualHeight);
Top = r.Bottom - ActualHeight;
};
The commented out section seems to work fine but the bottom doesn't, I also tried to change "ActualWidth/Height" to just normal.
6 Replies
Ryada
Ryada16mo ago
So first things first. Did you check if the values for the manual calculation are the ones being used during runtime? If you place a breakpoint on the line where you set Left during debugging it will pause execution there and you can read the values of the img and mainwindow that you are using Try seeing if any of those values aren’t matching the expected values
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Lep
Lep16mo ago
Sorry didn't get a chance to get on, none of the values are matching. My image properties are 234x480, if I leave it empty without trying to resize, the width is 1440, if I try to use MainWin.Width = img.ActualWidth; as a test, it stays as 1440, if I try to make it "img.Width" instead the Width value becomes 2.
Lep
Lep16mo ago
These are the values manually inserted, I would like to calculate it as: (img.ActualWidth * MainWin.Height / img.ActualHeight) but it doesn't seem like I can get the image's properties. Or if there's a better way of doing this, would be great, I can't put it automatic cause the actua; width value will always be 1440.
Lep
Lep16mo ago
Nevermind, found a solution. I was trying to use newimg to deal with the problem before but it wasn't working since I didn't have the images on my debug file so I was using "img" instead since it seems like it should of been working. I threw my Images folder inside the debug folder and called it a day to make it work lol. Still, if there's a better way of dealing with this, please, do let me know.
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
Implement string properties1, public string Prop { get; set} = string.Empty; 2, public string Prop { get; set} = ""; 3, public❔ Trying to use a listview but nothing updates in the viewi am trying to add a feature that groups files under user created groups. the buttons run and work. ❔ What is the difference between public static and public static virtual in a interface?This is a interface for dynamic plugins loaded from a `AssemblyLoadContext`. ```cs using System; us❔ Google Api- PermissionDeniedFacing an access problem using the google Api NuGet: ````Environment.SetEnvironmentVariable("GOOGLE❔ EF Core 7 - FromSql join with same column namesThis code produces ArgumentException -> An item with the same key has already been added. Key: Custo❔ WebSocketSharp: Identify WebSocket-SessionIs there a way in WebSocketSharp to identify the session by smth like an id in the ``OnMessage``- an❔ Do I need to take any extra steps to install a Windows service with Parallel.ForEach?Is there any additional step required to install a Windows service when utilizing Parallel.ForEach, ✅ How to break date into intervals?I have this: ```cpp /// <summary> /// List of intervals inside worktime /// </summary> [JsonProperty❔ Damn buttons.I want a button on a WinForm to change varibles to whatever the combobox has selected when the butto❔ Incremental source generator not called with dockerHello, I have 2 projects (simplified): - one sandbox .net6 - one SourceGenerator .netStandard2.0 I