How get Image from ImageStream? - c#

I'm getting ImageStream on CaptureImageAvailable event.
On ContentReadyEventArgs I have ImageStream.
I try copy this Stream to Image through BitmapImage:
Image cameraImage=new Image();
BitmapImage bImage=new BitmapImage();
private void CaptureImageAvailable(object sender, ContentReadyEventArgs e)
{
bImage.SetSource(e.ImageStream);
cameraImage.Source = bImage;
}
but I have error on step bImage.SetSource(e.ImageStream);
How I should read this ImageStream?
Thanks!

You cannot assign the Source property if you are not on the main thread. And in this case, you aren't. To do that, simply force invoking it using Dispatcher
Image cameraImage=new Image();
BitmapImage bImage=new BitmapImage();
private void CaptureImageAvailable(object sender, ContentReadyEventArgs e)
{
Dispatcher.BeginInvoke(()=>
{
bImage.SetSource(e.ImageStream);
cameraImage.Source = bImage;
});
}
In case you are not inside a page (e.g. you are inside view-model or some other non-UI class), use the following syntax:
Deployment.Current.Dispatcher.BeginInvoke(()=>
{
/* ... */
});

Related

C# UWP - How to get an object's drag image?

How can I get a copy of the drag image for an item I'm dragging in my application?
The DragStarting event contains a DragStartingEventArgs object that includes a DragUI object whose description is, "The visual representation of the data being dragged." Ideally, I'd like to do something like this within the DragStarting event:
private void OnDragStarting( UIElement sender, DragStartingEventArgs args )
{
// Create a new bitmap image object
var dragImage = new BitmapImage();
// Assign the drag image to the new bitmap image object
dragImage = args.DragUI. ????
}
However, there doesn't seem to be a way to get the drag image from the DragUI object. DragUI contains only "Set*" methods, and no "Get*" methods.
Is there a way to get the drag image as the drag operation begins?
You can use DataView.GetStorageItemsAsync() to receive the items you dragged into your applications.
<Grid AllowDrop="True" DragOver="Image_drop_drag_over_ui" Drop="image_drop"/>
//C# code
public async void Image_Drop(object sender, DragEventArgs e)
{
if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
List<StorageFile> received_images = new List<StorageFile>();
var items = await e.DataView.GetStorageItemsAsync();
var storageFile = items[0] as StorageFile;
received_images.Add(storageFile);
}
}
private void Image_drop_drag_over_ui(object sender, DragEventArgs e)
{
e.AcceptedOperation = DataPackageOperation.Copy;
e.DragUIOverride.Caption = "Drop receipt";
e.DragUIOverride.IsCaptionVisible = true;
}

Toggle the picture of picture box with picture box on_click c#

I have one image in my picture box using the resource what i want is to change the image when i click the icon.png it should be changed to icon1.png within the picturebox then when i click again the picture box it should be changed to icon.png
private void pictureBox10_Click(object sender, EventArgs e)
{
if (pictureBox10.ImageLocation != #"icon1.png")
{
var image = Image.FromFile(#"icon1.png");
pictureBox10.Image = image;
}
if (pictureBox10.ImageLocation == #"icon1.png")
{
var image = Image.FromFile(#"icon.png");
pictureBox10.Image = image;
}
}
but its not working please help me out of this.
You're getting a null from the image location as it's not set when you're assigning a picture to the Image property. There are a few ways to fix this:
Change the assignment so you assign using the ImageLocation
pictureBox10.ImageLocation = #"icon1.png";
Change the check to see if the Image property is equal to your new Image
pictureBox10.Image == Image.FromFile(#"icon.png");
Set the image location at the same time you set the image property
pictureBox10.Image == Image.FromFile(#"icon.png");
pictureBox10.ImageLocation = #"icon.png" ;
I feel the second might not come back as equal, you probably want to try the first one or third
Suggested code:
private void pictureBox10_Click(object sender, EventArgs e)
{
if (pictureBox10.ImageLocation != #"icon1.png")
{
pictureBox10.ImageLocation = #"icon1.png"
}
if (pictureBox10.ImageLocation == #"icon1.png")
{
pictureBox10.ImageLocation = #"icon.png";
}
}
Or:
private void pictureBox10_Click(object sender, EventArgs e)
{
if (pictureBox10.ImageLocation != #"icon1.png")
{
var image = Image.FromFile(#"icon1.png");
pictureBox10.Image = image;
pictureBox10.ImageLocation = #"icon1.png";
}
if (pictureBox10.ImageLocation == #"icon1.png")
{
var image = Image.FromFile(#"icon.png");
pictureBox10.Image = image;
pictureBox10.ImageLocation = #"icon.png";
}
}
You would also need to update your intial property setting to set the ImageLocation and not the Image property or to set the ImageLocation at the same time you set the Image file
EDIT
Off the top of my head, to set the property initially, you can do this (Source):
protected override void OnLoad(EventArgs e){
pictureBox10.ImageLocation = #"icon.png";
}
Though I can't remember if the PictureBox would have been created then, if not then use the onShown event instead (Source)
EDIT 2
Here is another way to create the event and set the property, first follow the steps here to add the event onShown to the form. You need to click on the form itself and not the controls inside the form to find the event.
Once done, inside the event add the following code:
pictureBox10.ImageLocation = #"icon.png";
That should help to resolve your issue
Try to directly reference the picture into the picture box:
pictureBox10.Image = Image.FromFile(#"Images\a.bmp");
Source
Thanks every one thanks a lot but there was some problem but have solved this issue so m writing the actual code...
public Form1()
{
InitializeComponent();
pictureBox10.ImageLocation = #"icon.png";
}
private void pictureBox10_Click(object sender, EventArgs e)
{
if (pictureBox10.ImageLocation == #"icon1.png")
{
pictureBox10.ImageLocation = #"icon.png";
}
else
{
pictureBox10.ImageLocation = #"icon1.png";
}
}
first you have to initialize the image location then use two if condition i think that was the main problem use if else anyway thaks every one thaks a lot special thaks to #Draken

PictureBox loses its image on navigating to different application

I started developing Windows Forms Applications recently. I'm working with PictureBox and I'm facing an issue with it. It is losing the image as shown in below pictures when I navigate out or if I minimize it and open it back. Any help is greatly appreciated.
private void button1_Click(object sender, EventArgs e) {
try {
using (FileStream fs = new FileStream("C:\\Users\\Public\\Pictures\\Sample Pictures\\Penguins.jpg", FileMode.Open, FileAccess.Read)) {
using (Image original = Image.FromStream(fs)) {
Bitmap image1 = (Bitmap)original;
pictureBox1.Image = image1;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Refresh();
}
}
}
catch (System.IO.FileNotFoundException) {
MessageBox.Show("There was an error opening the bitmap." +
"Please check the path.");
}
}
Forms Application before Navigating
Forms Application after Navigating to another app or minimizing it
This is because you are disposing the image
using (Image original = ....
{
Bitmap image1 = (Bitmap)original;
// ...
}
image1 is the same object as original, just cast to Bitmap, and gets disposed right after being painted on the screen (with Refresh method).
To fix the problem, use the following instead
Bitmap image1 = new Bitmap(original);
// ...

Convert to Image Object from OpenReadCompletedEventArgs Result

I'm busy with a windows phone application that calls a webservice that in turn returns a png image. In my code where I handle the returned result it looks like this:
void ImgDownloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
var graphImage = e.Result;
imgGraph.Source = graphImage; //Error here
//Stop loading animation
refreshProgressBar.IsIndeterminate = false;
}
But currently I'm getting an error (See comment) that says:
Cannot implicitly convert type IO.Stream to Media.ImageSource.
Would a normal type parse work here or should this be handled in another way?
Thanks in advance!
You should use the BitmapImage class:
void ImgDownloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
var graphImage = e.Result;
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(graphImage);
imgGraph.Source = bitmap;
//Stop loading animation
refreshProgressBar.IsIndeterminate = false;
}

How to notify PropertyChanged after Image Opened?

I am having some problems with my gallery. First I download all thumbnails for an overview. But when I click on a picture, first I want to show the thumbnail and load the big picture. When this is done, I want to change the ImageSource to the new picture. Here is my example:
private BitmapImage picture;
public BitmapImage Picture
{
get
{
if (picture == null)
{
RequestBigpicture();
return Thumbnail;
}
return picture;
}
}
public void RequestBigpicture()
{
picture = new BitmapImage(new Uri("http://www.fun-hollywood.de/" + bigPicture, UriKind.Absolute));
picture.ImageOpened += pictureImage_ImageOpened;
}
void pictureImage_ImageOpened(object sender, System.Windows.RoutedEventArgs e)
{
NotifyPropertyChanged("Picture");
}
This was not working, also this part (as I read somewhere) in RequestBitPicture is not better:
picture = new BitmapImage(new Uri("http://www.fun-hollywood.de/" + bigPicture, UriKind.Absolute));
var pictureImage = new Image();
pictureImage.Source = picture;
pictureImage.ImageOpened += pictureImage_ImageOpened;
The ImageOpened is never called. How would be the best way to do this?
I think you should set the BitmapImage.CreateOptions property to None or to BackgroundCreation to instantly trigger the image download.
Because the default value is DelayCreation that is why your image won't donwloaded and the ImageOpened event is never fired.

Categories