I have an Image object in my C# code and I'd like to use it as ImageSource for ImageBrush.
Is there a way to do this?
In other words, I need something like this:
Image image = new Image();
image.source = GetBitmapImage();
//execute various image transforms here...
ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = image; // this doesn't work
Thanks.
The ImageSource property is set to type of Windows.UI.Xaml.Media.ImageSource. Therefore you must provide an object that derives from Windows.UI.Xaml.Media.ImageSource.
Your object "image" is of type Windows.UI.Xaml.Controls.Image which is not derrived from the ImageSource type.
However, your method GetBitmapImage() returns a type of ImageSource so you can call the code below after you have finished your modifications.
imageBrush.ImageSource = image.Source;
Cheers.
You already have ImageSource - it is your GetBitmapImage(), so you can use
ImageBrush imageBrush = new ImageBrush(GetBitmapImage());
or use your image.source:
imageBrush.ImageSource = image.source;
Related
I am using in xaml with some fix image source, later on I wanted to use local image and wanted to assign to the xaml. But I am not able to use stretch in new image created .
Is there any way to use the stretch in other way ?
My code in xaml :
<Image Source="/Assets/Images/fix.png" Visibility="Visible" x:Name="ContentImage" Stretch="UniformToFill" />
and in C#
if (File.Exists(imagePath))
{
Image newImage = new Image();
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri(imagePath);
logo.EndInit();
newImage.Source = logo;
newImage.Stretch = Stretch.UniformToFill;
ContentImage.Source = newImage.Source;
}
It makes no sense to create another Image element. It is also not necessary to call Begin/EndInit. Use the BitmapImage constructor that takes an Uri argument instead.
This should be sufficient:
if (File.Exists(imagePath))
{
ContentImage.Source = new BitmapImage(new Uri(imagePath));
}
please tell me how to convert text or string in bitmap image in wpf in c#.because there is no bitmap class is available like windows form..
Thanks for Advance :)
BitmapImage is what you are looking for. Refer to the sample provided in MSDN link.
// Create the image element.
Image simpleImage = new Image();
simpleImage.Width = 200;
simpleImage.Margin = new Thickness(5);
// Create source.
BitmapImage bi = new BitmapImage();
// BitmapImage.UriSource must be in a BeginInit/EndInit block.
bi.BeginInit();
bi.UriSource = new Uri(#"/sampleImages/cherries_larger.jpg",UriKind.RelativeOrAbsolute);
bi.EndInit();
// Set the image source.
simpleImage.Source = bi;
Also, you can set the source directly in XAML which WPF internally convert to ImageSource via default converter for String to ImageSource.
<Image Source="/sampleImages/cherries_larger.jpg"/>
I have problem with setting Image Source property... So I tried use every variation of this code and for some reason it won't work.
When I set property manually it works, but when I want to change picture in code it is just blank.
`BitmapImage` bm = new BitmapImage();
bm.UriSource=new Uri(this.BaseUri,#"\\Assets\logo6.png");
this.image.Source = image;
I use this code inside of button event so I can change picture inside image control.
you are setting the path in wrong way. try any of these two options it will work.
Also make sure your image property Build Action is should set to Content and Copy To OutPut Directory to Copy to newer or Copy Always
BitmapImage bm = new BitmapImage();
bm.UriSource = new Uri(#"\Assets\Tiles\IconicTileSmall.png",UriKind.Relative);
image.Source = bm;
And
BitmapImage bm = new BitmapImage();
bm.UriSource = new Uri("\\Assets\\Tiles\\IconicTileSmall.png",UriKind.Relative);
image.Source = bm;
Basically you are mixing these two ways.
After looking for the answer in both Microsoft documentation and forums, I am at a loss. I am loading a png image as a background for an inkCanvas (WPF) which works fine, however, it always resizes the image to fit in the canvas, despite the image size..
Here was my last attempt with no success:
BitmapImage ii = new BitmapImage(new Uri(path));
Image img = new Image();
img.Stretch = Stretch.None;
img.Source = ii;
InkCanvas1.Background = new ImageBrush(ii);
Here is what it looks like using Stretch.None and Stretch.Fill
Here is what I am trying to achieve:
Can this be done?
The problem is that you're trying to set properties on an Image object that you're not using while ignoring the same settings on the ImageBrush that you are using. The Image in this case is just being thrown away and the ImageBrush just happens to be using the same source image. Set the Stretch property on the ImageBrush instead:
BitmapImage ii = new BitmapImage(new Uri(path));
ImageBrush imageBrush = new ImageBrush(ii);
imageBrush.Stretch = Stretch.Uniform;
InkCanvas1.Background = imageBrush;
I have tried this but I am not getting the answer. I have 3 image box. In this 2 image box have images and 1 image box is empty. Now I want to change the image on image_tab event, can any one help me to find this?
Uri myfile = new Uri("Images/star.png", UriKind.Relative);
StreamResourceInfo resourceInfo = Application.GetResourceStream(myfile);
BitmapImage myimage = new BitmapImage(myfile);
myimage.SetSource(resourceInfo.Stream);
image1.Source = myimage;
I am not sure what exactly image_tab is in your case (maybe Image_Tap?), but you can simplify your image source assignment by using:
Uri uri = new Uri("PATH", UriKind.Relative);
BitmapImage imageSource = new BitmapImage(uri);
image.Source = imageSource;
Remember, that the ImageSource object does not expose the path directly, so you might want to have an internal reference location where paths to files you use are handled.