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"/>
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));
}
I am trying to Crop a BitmapImage in my WPF application StageWindow.Stage using the CroppedBitmap class.
When saving the new cropped image to file JpegBitmapEncoder I can see the correctly cropped image. CroppedBitmap Crop
However I actually want to save the cropped image into a List\<BitmapImage\> for later use in another WPF image control. That list contains BitmapImages already so changing it to CroppedBitmaps would not suffice.
to be able to store the new croppedBitmap I use something like this:
BitmapImage xx = CroppedBitmap1 as BitmapImage;
when setting an WPF image control to the new BitmapImage (ExpectedCroppedImage) it still displays the CroppedBitmaps original source image without the crop.
I suspect the code above to delete the crop attribute from the new BitmapImage because the BitmapImage itself has no attributes for the cropped Area. But how can I save only the cropped part into a new BitmapImage?
I have been searching around but it seems CroppedBitmap should do the trick I just dont know how to convert it back in the correct manner.
Here is the Code for clarification:
//Stage is the WPF image element that im reading the source image from.
ImageSource StageWindowImageSource = StageWindow.Stage.Source;
CroppedBitmap Crop = new CroppedBitmap((BitmapSource)StageWindowImageSource, new Int32Rect(0,0,50,50));
ExpectedCroppedImage = Crop.Source as BitmapImage;
JpegBitmapEncoder jpg = new JpegBitmapEncoder();
jpg.Frames.Add(BitmapFrame.Create(Crop));
FileStream fp = new FileStream("F:/Crop.jpg", FileMode.Create, FileAccess.Write);
jpg.Save(fp);
fp.Close();
The line
ExpectedCroppedImage = Crop.Source as BitmapImage;
does not return the cropped bitmap, but the original one from which the crop was taken, i.e. the Source bitmap.
Besides that, you don't need any conversion from CroppedBitmap to BitmapImage.
Instead, use a base class of BitmapImage and CroppedBitmap as element type of the List, i.e. use a List<BitmapSource> or List<ImageSource> for your collection.
Now you can assign instances of any class derived from the element type.
List<BitmapSource> images = ...
CroppedBitmap croppedImage = new CroppedBitmap(...);
images.Add(croppedImage);
BitmapImage someOtherImage = new BitmapImage(...);
images.Add(someOtherImage);
private void MyMethod()
{
ImageSource StageWindowImageSource = img.Source;
CroppedBitmap Crop = new CroppedBitmap((BitmapSource) StageWindowImageSource, new Int32Rect(20, 20, 150, 110));
img2.Source = GetImage(Crop, new JpegBitmapEncoder());
}
private BitmapImage GetImage(BitmapSource source, BitmapEncoder encoder)
{
var image = new BitmapImage();
using (var sourceMs = new MemoryStream())
{
encoder.Frames.Add(BitmapFrame.Create(source));
encoder.Save(sourceMs);
sourceMs.Position = 0;
using (var destMs = new MemoryStream(sourceMs.ToArray()))
{
image.BeginInit();
image.StreamSource = destMs;
image.CacheOption = BitmapCacheOption.OnLoad;
image.EndInit();
image.Freeze();
}
}
return image;
}
The constructor of WriteableBitmap class with Windows 8 only takes two arguments: the height and the width of this object. Meanwhile with Silverlight it accepts a BitmapImage object as argument. (Verified on MSDN : WriteableBitmap.WriteableBitmap constructor)
I would like to load this BitmapImage because I'm trying to blur an image which already exists on my Assets folder.
Thanks for your help, I succeed to blur my image. Here is the sample in order to link the BitmapImage into the WriteableBitmap object :
BitmapImage bi = new BitmapImage(new Uri(filename, UriKind.RelativeOrAbsolute));
WriteableBitmap wb = new WriteableBitmap(bi.PixelWidth, bi.PixelHeight);
var streamFile = await GetFileStream(myFile);
await wb.SetSourceAsync(streamFile);
wb = wb.Convolute(WriteableBitmapExtensions.KernelGaussianBlur5x5);
Then just write the WriteableBitmap into the LocalStorage !
You should be able to load a BitmapImage into WritableBitmap like this:
WriteableBitmap writableBitmap = new WriteableBitmap(bitmapImage);
See here WriteableBitmap Constructor (BitmapSource)
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.
I'm new to WPF. I was programming for Windows Forms Applications, and there was Picture Box with Image property. But in WPF there is Image instead of Picture Box. How to get it's value? I mean the image data.
you can do that by using Image.Source property.
You will find a lot of samples on internet about it. For example code below sets image source, you can get the image similarly
Image myImage3 = new Image();
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri("smiley_stackpanel.PNG", UriKind.Relative);
bi3.EndInit();
myImage3.Stretch = Stretch.Fill;
myImage3.Source = bi3;
//get the source
BitmapImage imgSource = myImage3.Source;