How to notify PropertyChanged after Image Opened? - c#

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.

Related

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

Spawning images/pictureboxes

In C# WPF, I'm trying to create a new picturebox on the point where I clicked, whenever I click on the main window. I'm not too sure how to approach this since I could not find anything on the internet about this.
Are you sure you want to create new one each time? That could potentially be a bad idea. But still, if you want to, You can do it in codebehind like this:
public MainWindow()
{
InitializeComponent();
MouseUp += MainWindow_MouseUp; //add eventhandler vor click event
}
void MainWindow_MouseUp(object sender, MouseButtonEventArgs e)
{
var img = new Image(); //create new instance of image
img.Width = 100; //set some size properties
img.Height = 100;
img.Source = somesource;//set source
MainGrid.Children.Add(img); //add it as a child to some conteiner element, like grid.
}
But please reconsider reusing controls, if possible.

How get Image from ImageStream?

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(()=>
{
/* ... */
});

How to overlay two images in C#

So ive tried following a load of different tutorials on Stack and the internet on how to do this, but i'm getting nowhere.
this is the closest ive gotten:
private void button1_Click(object sender, EventArgs e)
{
Image image1 = Image.FromFile("S:\\Software\\C#\\Project\\WindowsFormsApplication1\\1.png");
Image image2 = Image.FromFile("S:\\Software\\C#\\Project\\WindowsFormsApplication1\\2.png");
using (Graphics g = Graphics.FromImage(image1))
{
g.DrawImageUnscaled(image2, 0, 0);
}
}
And when i click my button, it executes the code but literally nothing happens, why is this?
You have to do something with your image object afterwards. You have at least two possibilities:
Either save it back to a file using the Image.Save method, e.g.
image1.Save("S:\\Test.jpg");
or place a PictureBox on your Form and put it in there
PictureBox1.Image = image1;
...to place it in a new Window:
Form imgForm = new Form();
imgForm.BackgroundImage = image1;
imgForm.Show();

Changing a Windows Phone Application's WritableBitmap Object

All, I have a basic Windows 7 Phone application and I have just finished a crop page, where the users are able to crop an image taken with the phones camera. In the cameraCapTask_Completed event I set the App's global WritableBitmap
public static WriteableBitmap capturedImage;
as follows
void cameraCapTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK && e.ChosenPhoto != null)
{
// Take JPEG stream and decode into a WriteableBitmap object.
App.capturedImage = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
When I take a picture I then pass it to the cropping page in the CropProcessPage constructor I set the Image in the page via
public CropProcessPage()
{
InitializeComponent();
// Set the text and display captured image.
imageMain.Source = App.capturedImage;
This works. However, when I go back to the Main Page and retake/take another image, when I try to the new image, the old image (the first one taken) is shown. The constructor is being called and so is the camera captured event (setting the new image). What am I doing wrong here?
In CropProcessPage
move line
imageMain.Source = App.capturedImage;
to
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
// Set the text and display captured image.
imageMain.Source = App.capturedImage;
}

Categories