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;
}
Related
I have been trying to implement a ctrl z feature in my mini paint application.
The problem is that for the picture box side of things, it doesn't work. At the the beggining of the mouse up event before the new drawing, I save the current picture box image in a stack and when I press ctrl z, I pop that image and save it in the picture box image. The problem is that it always saves the new modified image in the stack not the previous one even if I saved it technically before the new updates (at the begining of the mouse up event).
A snippet of the code
void key(object sender, KeyEventArgs e){
...
else if ( e.Control && e.KeyCode == Keys.Z) {
if (save.Count > 0) {
//trying to save the previous image
pictureBox9.Image = save.Peek() as Image;
drawArea = save.Pop() as Bitmap;
pictureBox9.Image = drawArea;
g1 = Graphics.FromImage(drawArea);
// pictureBox9.Refresh();
}
}
}
private void pictureBox9_MouseUp(object sender, MouseEventArgs e){
save.push(pictureBox9.Image);
//save is the stack and that should be the current image
....
}
I have a browse button, and two empty images (image1, image2). I want to first click the browse button and load an image to (image1). On the second click I want to load the image to (image2).
I'm very new to WPF and C# in general. I guess I need some method to control the clicks of the button? Does anyone have any idea about this? I would highly appreciate it.
This is my code behind attempt:
private void button1_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
fd.DefaultExt = ".tif";
fd.Filter = "(*.tif,*.tiff)|*.tif;*.tiff";
fd.ShowDialog();
string fname = fd.FileName;
textBox1.Text = fname;
image1.Source = new BitmapImage(new Uri(fd.FileName));
}
After this, the first image is displayed in image1, but when I browse for another image it comes on top of image 1, and not in image2. How can I make the second image that I browse display in image2? maybe something like, if the button is already clicked one time, then the image should load into image2? or if image1 is already full then load to image2? I'm not sure!
Oh and the purpose of this is to create an interface that lets the user browse different images shown in a listbox, and when the user clicks each image, it displays in another window and the user can zoom in and out and so on.
But right now I'm just stuck with this small part of my project!
While I question the why you want to do such a thing, you could use the following. Also, please show some effort next time. This is a relatively easy solution!
private bool _ImageOneSet;
public MainWindow()
{
InitializeComponent();
_ImageOneSet = false;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (!_ImageOneSet)
{
// set image one
_ImageOneSet = true;
}
else
{
// set image two
}
}
I use a private field that is set to false. The first time the button_click event gets triggered, _ImageOneSet is still false, so you can set the first image.
The second (and third, fourth etc...), _ImageOneSet is true so you will set the second image.
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.
how can i use image in if statement?
for example i want to check in click event of a picturebox that if a specific image(for example image1) is in it,then do some work.thanks in advance
update:
like this
the picturebox can be null or have an image in it,i want to check image1 is in it or not
private void pictureBox34_Click(object sender, EventArgs e)
{
if (///picturebox34=image1)
{
f();
}
}
now when i compile this code i took error:cannot convert system.drawing.image to bool
If you have all the images loaded into memory then you could simply perform a reference comparison between the Image object assigned to the PictureBox and the other Image objects to determine which one is in the PictureBox.
Alternatively when you assign the Image to the PictureBox you can set the Tag property of the PictureBox to identify the Image and use the value of the Tag property for the test in your event handler. Of course in this case you only need the one Image in memory, but you will have to have some kind of identifying information, like the name of the image to assign to the Tag property.
Update: Based on your updated question, it seems you are happy to perform a refernce comparison. Which you can do as follows
private void pictureBox34_Click(object sender, EventArgs e)
{
if (picturebox34.Image == image1)
{
f();
}
}
OR - How To Shave A Koala To Stop It Looking Squashed. (But I didn't think that would make a suitably techy title)
The Problem: You have three preview images derived from a main image. The preview images are resized for standardised picture spaces on a company website, the main image can be any size image from anywhere.
Example: The main image is a hi-res image of a koala bear measuring 2000x2250. Your previews want to render the koala at 200x200, 200x50 and 250x150.
Your utility program resizes and stretches the original image to the size of your three "actual size" previews but obviously each preview looks a bit squashy and you know everyone hates to see a squashed koala.
To resolve this you add a little cropping method to your program which shaves five pixels from the preview on the desired side. This means you should be able to resize your image and unsquash your koala by shaving off the unnecessary parts of the image.
You add four buttons to each preview image picture box and create four generic methods for sending the correct shaving instructions to the crop method. You want to associate each specific button with a specific picturebox on the form, but you want to send all the click events to four generic functions.
How do you tell the generic function which of the three preview picturebox images you want it to shave in an elegant and wonderful way?
Example Code:
//cropPict=method for cropping the picture in the relevant picturebox.
//CropSide=a little enum which tells the method which side to crop.
private void btnT_Click(object sender, EventArgs e)
{
cropPict(/*Reference to PictureBox Goes Here*/, CropSide.Top);
}
private void btnB_Click(object sender, EventArgs e)
{
cropPict(/*Reference to PictureBox Goes Here*/, CropSide.Bottom);
}
private void btnR_Click(object sender, EventArgs e)
{
cropPict(/*Reference to PictureBox Goes Here*/, CropSide.Right);
}
private void btnL_Click(object sender, EventArgs e)
{
cropPict(/*Reference to PictureBox Goes Here*/, CropSide.Left);
}
EDIT: As it happens, inspired by Hans below, rather than just stuffing the PictureBox into the tag. Which was a great idea I actually put a KeyValuePair into the tag for each button like so:
btnCCB.Tag = new KeyValuePair<CropSide,PictureBox>(CropSide.Bottom,pbxKoala);
btnCCL.Tag = new KeyValuePair<CropSide, PictureBox>(CropSide.Left, pbxKoala);
btnCCR.Tag = new KeyValuePair<CropSide, PictureBox>(CropSide.Right, pbxKoala);
btnCCT.Tag = new KeyValuePair<CropSide, PictureBox>(CropSide.Top, pbxKoala);
Then I could just wire all the buttons up to a single event handler like so:
private void btnC_Click(object sender, EventArgs e)
{
Button btnSend = (Button)sender;
KeyValuePair<CropSide, PictureBox> kvCrop = (KeyValuePair<CropSide, PictureBox>)btnSend.Tag;
cropPict(kvCrop.Value,kvCrop.Key);
}
Of course, there's still plenty more to do but that pretty much sorted out my problem. Thanks Hans!
Use the Button.Tag property to store a reference to its associated PictureBox. Cast sender to Button:
public Form1()
{
InitializeComponent();
button1.Tag = pictureBox1;
button1.Click += btnT_Click;
// etc..
}
private void btnT_Click(object sender, EventArgs e)
{
var btn = (Button)sender;
cropPict((PictureBox)btn.Tag, CropSide.Top);
}