Display image depending on application theme (Dark/light theme) - c#

I am develop an UWP app, and I am using template10. I have two images, one white and other black. I want show black image in light theme, and white image in dark theme. I have this code:
if (this.RequestedTheme == ElementTheme.Light)
Image.Source = new BitmapImage(new Uri("ms-appx:///Assets/BlackImage.png"));
else
Image.Source = new BitmapImage(new Uri("ms-appx:///Assets/WhiteImage.png"));
But, when I choose light theme image dont appear! But when I choose dark theme, white image appears.

If we do not set the ElementTheme.Light or ElementTheme.Dark to the FrameworkElement.RequestedTheme, it will always return the ElementTheme.Default. So your Image will be set with WhiteImage, no matter the ApplicationTheme is Light.
The RequestedTheme in app that can gets or sets a value that determines the light-dark preference for the overall theme of an app. It returns ApplicationTheme of the enumeration. It includes Light and Dark. We should be able to use App.Current.RequestedTheme to get the current ApplicationTheme of the App.
For example:
var AppRequestedTheme = App.Current.RequestedTheme.ToString();
if (AppRequestedTheme == "Light")
Image.Source = new BitmapImage(new Uri("ms-appx:///Assets/BlackImage.png"));
else
Image.Source = new BitmapImage(new Uri("ms-appx:///Assets/WhiteImage.png"));

Related

get the highest qulity image/screenshot C# wpf

I'm writing a little program that tranfers a screen shot of a user screen to my wpf image control but when the image is displayed it is not full,not the whole screen is displayed even if the action was done through my computer and not my leptop and it is blurry
Bitmap bmp=
new Bitmap((int)SystemParameters.PrimaryScreenWidth(int)SystemParameters.PrimaryScreenHeight);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(0, 0, 0, 0, bmp.Size);
the code above is the code to take the screenshot
BitmapImage bmI = new BitmapImage();
bmI.BeginInit();
bmI.UriSource = new Uri(FullPath);
bmI.EndInit();
Screen_Shot.Source = bmI;
And this code is the code to dislpay the image, Screen_Shot is the image name, and the full path is where i put the bitmap image
I tried to use
Screen_Shot.Stretch = Stretch.Fill; and UseLayoutRounding="True" SnapsToDevicePixels="True"
but none of them seems to get the job's done
this is an exsample of a screenshot i took from my own pc
You want to set the Stretch property to Uniform or else your image will be distorted as it will fill regardless of the Image control's size/shape. You also have to remember that the screen size may be different than your programs window size. If you don't uniformly stretch the image it will distort almost always (even if it's just a bit).
Regarding your "blurry" image, you need to specify the rendering option of you Screen_Shot image control or it will set the image to a lower quality that it can render more easily. This is honestly better to just set in the xaml. I normally use Fant, but look other options specified here.
<Image Name="Screen_Shot" RenderOption.BitmapScalingMode="Fant"/>

UWP C# Fill Rectangle with Image

My program has a rectangle that I color depending on the color from a color picker. What I want to do is to have it choose an image for black instead of just showing black. The code I have to fill the rectangle with the image is:
p1rect1.Fill = new ImageBrush
{
ImageSource = new BitmapImage(new Uri(#"pack://application:,,,/LED;Assets/Images/off.png", UriKind.Absolute))
};
I located this code from a c# wpf discussion, but it doesn't seem to be working in mine, which is UWP. What happens is that the rectangle doesn't change at all and is left as its original color when it was created in the xaml code.
What would the proper code be to put an image into a rectangle in C# code? Thanks.
You need to use the ms-appx scheme to reference files in your app package.
p1rect1.Fill = new ImageBrush
{
ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/Images/off.png"))
};

Darken Frame Background on Desktop UWP with XAML & C#

I am using user input images as the Frame Background in my app and I want to darken the image. Reducing opacity does the opposite and brightens the image as the Window color is white on Desktop and not black and I can't see any function to change the color of window.
Changing TitleBar Color doesn't work.
Changing the RequestedTheme from Light to Dark doesn't change the color.
Processing the image to darken is too slow and can't be used.
Here's the code to change the Frame background where 'image' is my BitmapImage object.
ImageBrush b = new ImageBrush();
b.ImageSource = image;
b.Stretch = Stretch.UniformToFill;
(Window.Current.Content as Frame).Background = b;
I need to use the Background on Frame and not on Page as navigation to other pages needs to be smooth and keep the background in place.

Setting ImageSource in Coding4Fun RoundButton Not Scaling

I am trying to create a button that is the exact same look and feel as a regular Windows Phone 8 application bar button. I am using the Coding4Fun RoundButton for this. My issue is that when setting the ImageSource I cannot get the image to scale to fit inside of the button properly, it is far too big. I am using a standard application bar icon image from the Windows Phone 8 SDK, whch is 76x76. I've also resized to mimic the Windows Phone 7 icon size of 48x48 but the same result occurs. How can I adjust the image so it fits in the RoundButton properly?
XAML
<c4f:RoundButton x:Name="browseRoundButton" Click="Browse_Click"/>
XAML.CS
if (Settings.LightTheme.Value)
browseRoundButton.ImageSource = new BitmapImage(new Uri("/Assets/Icons/feature.search.light.png", UriKind.Relative));
else
browseRoundButton.ImageSource = new BitmapImage(new Uri("/Assets/Icons/feature.search.dark.png", UriKind.Relative));
EDIT* In referencing sources like http://www.geekchamp.com/articles/wp7-roundtogglebutton-and-roundbutton-in-depth and http://coding4fun.codeplex.com/discussions?searchText=roundbutton I cannot seem to find any solutions.
My solution to this was to set the ImageSource in the XAML (using only the dark style icon) and ensure the size is 48x48. Works Great!

Align an image to the left in a UINavigationBar

I want to show a background image on my app's navigation bar.
Showing the image works:
UIImage image = new UIImage( #"Images/navbarlogo2.png");
UIImageView imageview = new UIImageView(image);
NavigationItem.TitleView = imageview;
But the image is centered, while I need it to be right-aligned.
I have not been able to find a property to do this, and searching yielded no answers.
You could use the following method to set your navbar's background image. Note that this will set it as the default for all the nav bars and is available since iOS5.
UINavigationBar.Appearance.SetBackgroundImage(image, UIBarMetrics.Default);

Categories