UWP C# Fill Rectangle with Image - c#

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"))
};

Related

Layering transparent images and text on top of each other?

I'm still pretty new to Visual Studio. I have a few transparent images that I want to later on top of each other so that they appear as one image when put together. On top of that, I'm using DrawString to draw text on top of the images. I've been using DrawImage to display the images into a PictureBox, but I can't seem to get them to all display at once.
Edit: Here's some of the code I've been using.
private void drawTextBox()
{
var image = new Bitmap(this.pictureBox_TextBox.Width, this.pictureBox_TextBox.Height);
var graphics = Graphics.FromImage(image);
graphics.DrawImage(new Bitmap(#"C:\Users\Home\Documents\Visual Studio 2013\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Resources\TextBox.png"), new Point(0, 151));
this.pictureBox_TextBox.Image = image;
}

Xamarin: How do I place an image on top of another image at a specific pixel location

I have an app I need to build with Xamarin for iOS. I'm using Xamarin.Forms at the moment.
The app is a custom view of a garden that I will need to place plant markers on in the admin at a touch. I cannot find a way to composite these images in a way I can drive the display from a database and make the markers clickable.
Any help or guidance is appreciated.
In this case you need to use AbsoluteLayout
var layout = new AbsoluteLayout();
var image = new Image { Source = "foo.png" };
AbsoluteLayout.SetLayoutBounds (image , new Rectangle (.5, 1, .5, .1));
//("X, Y, Width, Height")
layout.Children.Add (bottomLabel);
Content = layout;
here just a sample of placing image from c#, you can customize this to your requirement

Display image depending on application theme (Dark/light theme)

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"));

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

WPF Rectangle ImageSource

i couldnt fill rectangle object with image.
ImageBrush ib = new ImageBrush();
BitmapImage bmi = new BitmapImage(new Uri(#"/WpfImageApp;component/Images/Penguins.jpg", UriKind.Relative));
ib.ImageSource = bmi;
body.Fill = ib;
body is Rectangle object and when i run this code, there is only black screen on window. I also couldnt see my other controllers(like buttons). any solution?
After copying what you've done I can confirm that if the BitmapImage fails to load in the constructor you get a black window with no controls visible.
Move your code to a button that you can click AFTER the window has loaded and you'll see the correct error message.
Could not find a part of the path 'C:\WpfImageApp;component\Images\Penguins.jpg'.
After changing your URI to pack://application:,,,/Images/Penguins.jpg the image loads correctly

Categories