WPF Rectangle ImageSource - c#

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

Related

C# WPF when image is loaded the width and height are switched

In WPF c# loading an image from code in runtime, some images are loading when the width and height are switched so the width=height and height=width so the image is turned in 90 degrees.
In the file system properties tab, the Width and Height are correct and the image is shown correctly. If I open this image in any imageViewer the image is shown correctly but if I open it in powerpoint the same issue is found and the image is turned.
I downloaded some other codes in WPF and all are showing the image turned.
most images are shown correctly.
For example This Image :
https://www.dropbox.com/s/5fki0ew3gt78myi/TestImage_Widht4000_Height6000.JPG?dl=0
The Widht=4000and Height=6000 but if I get the bitmap.PixelWidth=6000 and bitmap.PixelHeight=400
Can anyone please help!
Thanks
I have tried everything :(
var ImagefilePath = SelectedImagePath + "\\" + ((object[])(e.AddedItems))[0].ToString();
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(ImagefilePath.ToString(), UriKind.Absolute);
bitmap.EndInit();
// Set Image.Source
imgPhoto.Source = bitmap;
ImageInfoText.Content=" W=" + bitmap.PixelWidth + " H=" + bitmap.PixelHeight;
It is most probably the metadata within the image itself. You need to get rid of the metadata and that should fix your problem.
Here is a similar issue : WPF some images are rotated when loaded
Here is how you can remove metadata: Easy way to clean metadata from an image?
The preferable way would be to use the System.Windows.Media.Imaging.BitmapEncoder class's Metadata collection property.

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

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

C# Winforms Get a Form's Stretched Background Image

How can I retrieve a form's stretched image? When I use MyForm.BackgroundImage it gives me the original image, but not the stretched image that is being displayed on the form.
If I cannot get the image, can I recreate the resulting image from BackgroundImageLayout = Stretch?
Why I want to do this:
I have a control that does not allow transparency. In order to fake transparency I take the background image and create an image for the section that the control covers. This works well until I set the BackGroundImageLayout to anything other than none.
Any help is greatly appreciated, thanks.
You can retrieve/re-create the stretched form background image like so:
private Bitmap getFormBackgroundImage()
{
Bitmap bmp = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(this.BackgroundImage,
new Rectangle(0, 0, bmp.Width, bmp.Height));
}
return bmp;
}
Then you can crop a portion of it to use as the child control's background.
Try this :
a) Set the background image of the form (Base Control).
this.BackgroundImage = <image>;
b) Create a child control and drop it over over your base control.
c) Set the dock of your child control to Fill.
this.childControl.Dock = DockStyle.Fill;
d) In the base control constructor , set the background image for the child control as the image of the base control. Like this:
childControl.BackgroundImage = this.BackgroundImage;
e) Set the background image layout of the child control to strech.
childControl.BackgroundImageLayout = ImageLayout.Stretch;
This would make your child control appear as transparent. Hope it solves your problem.

Align button background image

I have a button in my WinForms app and I added an image and text to it. I aligned the text to right and wanted to align the Background image to left but found out that it is not possible.
Is there any way to do that?
I have also tried to set just Image on the button but that couldn't be resized in the Button Properties.
May someone help me solve this out? Thanks so much.
In case that it is not possible I would have to resize every image in mspaint.
This is the result (as Background):
I need the BackgroundImage align to left.
This is result as Image when using align (not possible to resize)
Use Image property to set your image (make sure it fits button height, you can change image size if you will open it from project resources folder)
Set ImageAlign to MiddleLeft
Set TextAlign to MiddleRight
Do not change anything else. I.e. TextImageRelation should be Overlay. Result:
Set these properties of Button.
ImageAlign to MiddleRight
TextImageRelation to ImageBeforeText
TextAlign as MiddleCenter
To have it resized on Button. See below:
Bitmap image = Bitmap.FromFile(oFile) as Bitmap;
Bitmap resized = new Bitmap(image, new Size(30, 30));
button1.Image = resized;
button1.Text = "Button";
button1.ImageAlign = ContentAlignment.MiddleLeft;
button1.TextImageRelation = TextImageRelation.ImageBeforeText;
button1.TextAlign = ContentAlignment.MiddleRight;
You can use the Image property instead of the BackgroundImage. You can set the align later using the ImageAlign property.

Categories