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;
}
Related
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;
}
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
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(()=>
{
/* ... */
});
Hello and thanks for reading my thread! I would like to seek advice for my code because after a lot of searching I couldn't find anything to solve this particular problem. I've googled and searched on stackoverflow and all the solutions somehow didn't work (or I didn't know how to implement them). Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Threading ;
namespace Motion_Detection
{
public partial class Form1 : Form
{
private FilterInfoCollection VideoCaptureDevices;
private VideoCaptureDevice FinalVideo;
private void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap video = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = video;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo VideoCaptureDevice in VideoCaptureDevices)
{
devicesList.Items.Add(VideoCaptureDevice.Name);
devicesList.SelectedIndex = 0;
}
}
private void button1_Click(object sender, EventArgs e)
{
FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[devicesList.SelectedIndex].MonikerString);
FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
FinalVideo.Start();
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox1.Image = null;
FinalVideo.Stop();
}
private void button3_Click(object sender, EventArgs e)
{
worker.RunWorkerAsync();
}
private void button4_Click(object sender, EventArgs e)
{
worker.CancelAsync();
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
while (!worker.CancellationPending)
{
Bitmap map = new Bitmap(pictureBox1.Image); // this line throws the error
for (int x = 0; x < pictureBox1.Width; x++)
{
for (int y = 0; y < pictureBox1.Height; y++)
{
Color pixel = map.GetPixel(x, y);
if (pixel.R == 255 && pixel.G == 0 && pixel.B == 0)
{
// detected red pixel
}
}
}
Thread.Sleep(100); // check every 100ms or any other given interval
}
}
}
}
So I'm using the Aforge video dlls to access my web camera. This part works, I can access and read the stream from it, when I dump it into picturebox1 it shows up perfectly, without any lag whatsoever.
Now I'm toying a bit around with motion detection and for the first, I wanted to see if I can detect a pixel of a certain color appearing in front of the camera. Because I need to loop through every pixel, I had to put this on a different thread else it kept freezing my GUI and the display started to lag.
The issue is because I did this, I don't know how to properly access the picturebox.image content from the background worker without triggering the error from the title. Some people on the internet suggested using lock() but I never did this nor I know what I should lock() here. I never worked with multithreading before just because in the end I could never handle the access violations..
To fix this problem I tried things like try finally block although even within the try block, I've gotten the same exception. I assume that there is a cleaner way to do what I've mentioned but I can't really get my head around which one that might be.
I hope that my first post here on the forums was clear and understandable as possible.
Thanks in Regards
~ Ilhan
You cant/shouldn't access pictureBox1 unless on the UI thread.
I think you need to do something like this:
private void GetImage(out Bitmap img)
{
img = new Bitmap(pictureBox1.Image);
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
Bitmap img = null;
Invoke(new Action(() => GetImage(out img)));
// Do what you want with the bitmap
}
Accessing a control on a winform will throw an exception if its not on the UI thread.
You can find out if you are on the correct thread with pictureBox1.InvokeRequired.
Calling Invoke will send a message to the UI thread to do the passed delegate and then it will wait for the passed delegate to complete. Calling BeginInvoke will send the message but not wait.
How can I set a picture to a PictureBox in code?
The code below gives me the error:
Cannot implicitly convert Bitmap to
String.
private void ptbLocalidadAdd_MouseEnter(object sender, EventArgs e)
{
ptbLocalidadAdd.ImageLocation = Properties.Resources.addg;
}
If the resource is a bitmap, this should work:
ptbLocalidadAdd.Image = Properties.Resources.addg;