Windows phone picturebox - c#

I'm developing an windows phone 8.1 application in C#. I'm using the camera to take a picture. The picture is than saved on the device and I'm trying to show it in a picturebox.
I have tested it on a HTC phone and it worked nice, but when i tried it on a Nokia Lumia the picture would never load.
Does anyone have an idea how to solve that?
Here is the code I am sing to take a picture:
private void snap_task_Click(object sender, EventArgs e)
{
cameraCaptureTask = new CameraCaptureTask();
cameraCaptureTask.Completed += cameraCaptureTask_Completed;
cameraCaptureTask.Show();
}
void cameraCaptureTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
NavigationService.Navigate(new Uri("/Slika.xaml?fotka=" + e.OriginalFileName, UriKind.Relative));
}
}
And this is the code where I try toload the picture.
public Slika()
{
InitializeComponent();
string slika = string.Empty;
string slika2 = string.Empty;
this.Loaded += (s, e) =>
{
if (NavigationContext.QueryString.TryGetValue("fotka", out slika))
{
putanja = slika; /*"/Resources/" + slika + ".png";/**/
int x = putanja.Length;
if (x == 1)
{
putanja = "/Resources/" + putanja + ".png";
uriPutanja = new Uri(putanja, UriKind.Relative);
fotka = new BitmapImage(uriPutanja);
}
else
{
uriPutanja = new Uri(putanja, UriKind.Relative);
porukaTextBox.Text = putanja;
fotka = new BitmapImage(uriPutanja);
}
}
img1.Source = fotka;
};
}
PS
the loading from local resources works fine on both phones, it is just the "else" part of the if that is causing problems on the Nokia.

You are saving the image in the Camera roll folder in your phone, try saving it on your memory card instead and try if that works (you an just change it in the setting of the phone and say to save the new pictures on the SD card) If that works, try using the PhotoChooserTask in order to get the image.
I hope that the following code will help you:
using Microsoft.Phone.Tasks;
using System.IO;
using System.Windows.Media.Imaging;
...
PhotoChooserTask selectphoto = null;
private void button1_Click(object sender, RoutedEventArgs e)
{
selectphoto = new PhotoChooserTask();
selectphoto.Completed += new EventHandler(selectphoto_Completed);
selectphoto.Show();
}
void selectphoto_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
BinaryReader reader = new BinaryReader(e.ChosenPhoto);
image1.Source = new BitmapImage(new Uri(e.OriginalFileName));
}
}

You can try changing the UriKind from Relative to Absolute. If I have understood your code, you will get the absolute path to the picture.

As I understand the code you have forgotten the .png in the else case.

Related

How to detect when the image appears in PictureBox

I have a problem about System.Windows.Forms.PictureBox. I want to show a image on monitor and capture it on the camera. So I use Winform which include a picturebox. The picture box is:
PictureBox pb = new PictureBox();
pb.WaitOnLoad = true;
When I set a bitmap to PictureBox and capture the image from camera,
// Show bmp1
this.image.Image = bmp1;
this.image.Invalidate();
this.image.Refresh();
// Delay 1s
UseTimerToDelay1s();
// Show bmp2
this.image.Image = bmp2;
this.image.Invalidate();
this.image.Refresh();
// Capture
CaptureImageFromCamera();
It only capture the bmp1.
If I add a small delay like this,
this.image.Image = bmp2;
this.image.Invalidate();
this.image.Refresh();
UseTimerToDelay100ms();
CaptureImageFromCamera();
It capture bmp2. The Image set method seem to be a async method. Does any method to confirm the image is set? Thanks.
I'd use the first Paint event after assigning the new Image.
You can give it a try using a very large image url from this site.
The example uses google logo image url. Copy the following code and make sure you assign event handlers to the events:
bool newImageInstalled = true;
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.WaitOnLoad = true;
}
private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
if (!newImageInstalled)
{
newImageInstalled = true;
BeginInvoke(new Action(() =>
{
//Capturing the new image
using (var bm = new Bitmap(pictureBox1.ClientSize.Width,
pictureBox1.ClientSize.Height))
{
pictureBox1.DrawToBitmap(bm, pictureBox1.ClientRectangle);
var tempFile = System.IO.Path.GetTempFileName() + ".png";
bm.Save(tempFile, System.Drawing.Imaging.ImageFormat.Png);
System.Diagnostics.Process.Start(tempFile);
}
}));
}
}
private void button1_Click(object sender, EventArgs e)
{
newImageInstalled = false;
pictureBox1.ImageLocation =
"https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png";
}
private void button2_Click(object sender, EventArgs e)
{
newImageInstalled = false;
pictureBox1.Image = null;
}

No webcam picture with AForge in C#

I am just learning C# and I was trying to implement a webcam picture capture program. I'm using the Aforge library, the thing is that my picturebox is not displaying the webcam image and I don't understand why. If anyone knows my error, please let me know. Thank you in advance.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DarrenLee.Media;
namespace test4
{
public partial class MainForm : Form
{
int count = 0;
Camera myCamera = new Camera();
public MainForm()
{
InitializeComponent();
GetInfo();
myCamera.OnFrameArrived += myCamera_OnFrameArrived;
}
private void GetInfo()
{
var cameraDevices = myCamera.GetCameraSources();
var cameraResolutions = myCamera.GetSupportedResolutions();
foreach (var d in cameraDevices)
cmbCameraDevices.Items.Add(d);
foreach (var r in cameraResolutions)
cmbCameraResolutions.Items.Add(r);
cmbCameraDevices.SelectedIndex = 0;
cmbCameraDevices.SelectedIndex = 0;
}
private void myCamera_OnFrameArrived(object source, FrameArrivedEventArgs e)
{
Image img = e.GetFrame();
picCamera.Image = img;
}
void ComboBox1SelectedIndexChanged(object sender, EventArgs e)
{
myCamera.ChangeCamera(cmbCameraDevices.SelectedIndex);
}
void ComboBox2SelectedIndexChanged(object sender, EventArgs e)
{
myCamera.Start(cmbCameraDevices.SelectedIndex);
}
void Form1FormClosing(object sender, FormClosingEventArgs e)
{
myCamera.Stop();
}
void BTTsaveClick(object sender, EventArgs e)
{
string filename = Application.StartupPath + #"\" + "Image" + count.ToString();
myCamera.Capture(filename);
count++;
}
}
}
Picture of how it looks when I compile it:
https://i.gyazo.com/6956a07405cd4bf5e74c20bc321bd32e.png
I am connecting the picture box with the content in this line:
Image img = e.GetFrame();
picCamera.Image = img;
Make sure you click on the object before writing the code.
Example: Click on the button in the design form then paste the code for BTTsaveClick in the newly created method after clicking on the button.
If you are using Visual Studio, you can check if the methods are referenced. If it's not referenced, that means the code won't be read.

Using Tesseract OCR in C#

I have trying to deploy tesseract for reading the clipboard image through the code below in a C# window.form. But, a black commandline window appears and nothing happens.
private void b1_Click(object sender, EventArgs e)
{
if (ofd1.ShowDialog() == DialogResult.OK)
{
var img = new Bitmap(ofd1.FileName);
var ocr = new TessBaseAPI("./tessdata", "eng", OcrEngineMode.DEFAULT);
var page = ocr.SetImage(img);
tb1.Text = page.ToString();
}
}
The error it gives is cannot convert from 'System.Drawing.Bitmap' to 'Leptonica.Pix' hope this can be improved.
Instead of creating a bitmap, try using a Pix object like:
var img = Tesseract.Pix.LoadFromFile(ofd1.FileName)

Adding progresindicator to image downloading

I've got application page where is view from street camera. Photo form camera is refreshing about every 30 sec.
I created a button which is used to refresh photo.
I want to add progress indicator which will be show every time when photo is downloading.
I don't know how and where exactly I have to add code.
I tried many examples but fail.
Because I don't really understand how to turn it on and off.
public void downloading()
{
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(ImageOpenReadCompleted);
webClient.OpenReadAsync(new Uri("http://przeprawa.swi.pl/cgi-bin/kam.cgi?6&1399042906515&" + Guid.NewGuid()));
}
private void ImageOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(e.Result);
image1.Source = bmp;
}
}
public void Refresh(object sender, EventArgs e)
{
downloading();
}
Make this change to your code:
public void downloading()
{
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(ImageOpenReadCompleted);
webClient.OpenReadAsync(new Uri("http://przeprawa.swi.pl/cgi-bin/kam.cgi?6&1399042906515&" + Guid.NewGuid()));
var _progressIndicator = new ProgressIndicator
{
IsIndeterminate = true,
IsVisible = true,
Text = "Downloading...",
};
SystemTray.SetProgressIndicator(this, _progressIndicator);
}
private void ImageOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(e.Result);
image1.Source = bmp;
var _progressIndicator = new ProgressIndicator
{
IsVisible = false,
};
SystemTray.SetProgressIndicator(this, _progressIndicator);
}
}
public void Refresh(object sender, EventArgs e)
{
downloading();
}

How to pop up camera using picture box when button click?

I make a program in C# windows form I have tons of function in my form including two datagrid view that connected to dabase and including a camera that direcly connected to my PC I use AForge dll reference to connect to the camera device I just found the tutorial on youtube and it works perfecly for me, as I stated earlier I have too many programs in one form including that camera and it went out that the camera was need to be resized to a small resolution, so I decided to make a popup button that must show the wider resolution when I click the button on my form.
this is the code for my camera.
//Camera
// get the devices name
private void getCamList()
{
try
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
comboBox1.Items.Clear();
if (videoDevices.Count == 0)
throw new ApplicationException();
DeviceExist = true;
foreach (FilterInfo device in videoDevices)
{
comboBox1.Items.Add(device.Name);
}
comboBox1.SelectedIndex = 0; //make dafault to first cam
}
catch (ApplicationException)
{
DeviceExist = false;
comboBox1.Items.Add("No capture device on your system");
}
}
//refresh button
private void refresh_Click(object sender, EventArgs e)
{
getCamList();
}
//toggle start and stop button
private void start_Click(object sender, EventArgs e)
{
if (start.Text == "&Start")
{
if (DeviceExist)
{
videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
CloseVideoSource();
videoSource.DesiredFrameSize = new Size(160, 120);
//videoSource.DesiredFrameRate = 10;
videoSource.Start();
lblCam.Text = "Device running...";
start.Text = "&Stop";
}
else
{
lblCam.Text = "Error: No Device selected.";
}
}
else
{
if (videoSource.IsRunning)
{
CloseVideoSource();
lblCam.Text = "Device stopped.";
start.Text = "&Start";
}
}
}
//eventhandler if new frame is ready
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap img = (Bitmap)eventArgs.Frame.Clone();
//do processing here
pictureBox1.Image = img;
}
//close the device safely
private void CloseVideoSource()
{
if (!(videoSource == null))
if (videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource = null;
}
}
//prevent sudden close while device is running
private void Form1_FormClosed(object sender, FormClosingEventArgs e)
{
CloseVideoSource();
}
} }
I also posted a picture so that you have further understanding what I am talking.
as you can see at the lower right corner I have a pop up button there honestly telling you I already tried different methods but nothing works unfotunately I cannot post what I've tried because I created it yesterday and can no longer undo the codes I tried. Any Idea?
Create a new Form
Place a PictureBox on this Form
Add all methods to initialize and get the current frame to the Form (should be refactored to an own class providing an event like FrameChanged giving you the current image)
add a method like to the Form
public void ShowCamPopup(string deviceName)
{
InitializeDevice(string deviceName, int width, int height);
this.Show();
this.BringToTop();
}
You should the really consider to refactor the communication with your cam to an own class which reduces duplicate code and allows you to do performance tweaks (which you will need for sure later) at a single position of your solution.

Categories

Resources