Code is not executed in server side? - c#

The code tested and working in local computer but not in server side. So, i use log4net to see what happen, and here is what the log4net produce: http://pastebin.com/Xr3iq68t As you all can see, i put log4net almost in every line of my code.
What is the differences between my local computer with server(i'am using somee.com free hosting to test the code).
If it because of permission problem, i have tested another code which is downloading a snapshot from same source and save it in server side folder and it can save it.
So, is there anything wrong with the code?
The code executed until Rec class only, not proceed to the rest of the code. It should go to video_NewFrame after that, but from log file record, it not continue.Complete code can be viewed here: http://pastebin.com/VCjVj3uc
protected void Button1_Click(object sender, EventArgs e)
{
string usr = "http://IPaddress.com:8081/snapshot.cgi";
log.Info("DDNS: " + usr);
log.Info("Starting JPEG stream...");
//camera source
JPEGSource = new JPEGStream(usr);
log.Info("***JPEGSource = new JPEGStream(usr);***");
JPEGSource.Login = login;
JPEGSource.Password = password;
log.Info("***JPEG login & password***");
JPEGSource.Start();
log.Info("JPEGSource.Start()");
JPEGSource.NewFrame += new NewFrameEventHandler( video_NewFrame );
log.Info("***Goto video_NewFrame class***");
//System.Threading.Thread.Sleep(6000);
//Label2.Text = Label2.Text + streamingSource + "<br> ";
this.Label1.Text = "Camera Source: " + usr;
}
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap image = (Bitmap)eventArgs.Frame.Clone();
log.Info("***Bitmap image = (Bitmap)eventArgs.Frame.Clone();***");
width = image.Width;
height = image.Height;
frames.Enqueue((Bitmap)image.Clone());
log.Info("***Cloning frame***");
if (!IsRecording)
{
log.Info("Entering thread");
IsRecording = true;
Thread th = new Thread(DoRecording);
th.Start();
}
}
Note: camera Ip address is DDNS ip address.

Related

delete folder but the process cannot access the file because being used

so i made a simple project where when i click a button the picture edit get an image from a folder file, but when i want to delete the folder that contains the image, it gives me an error. the code as following
private void button1_Click(object sender, EventArgs e)
{
string pathx = AppDomain.CurrentDomain.BaseDirectory + "\\TempImage\\" + "naruto" + ".png";
pictureEdit1.Image = Image.FromFile(pathx);
}
private void button2_Click(object sender, EventArgs e)
{
string dir = AppDomain.CurrentDomain.BaseDirectory + "\\TempImage";
try {
if (Directory.Exists(dir))
{
//////give me an error in here///////
Directory.Delete(dir, true);
}
else
{
MessageBox.Show("folder not found");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
the purpose of this, is in my main project, for cache purpose. so i get an image from a certain folder after coping it from server to local. and when i want to close the main project i need to clear the cache or folder
Update
which is better alternate 1 or alternate 2 (to dispose)
private void button1_Click(object sender, EventArgs e)
{
string pathx = AppDomain.CurrentDomain.BaseDirectory + "\\TempImage\\" + "naruto" + ".png";
//alternate1
using (FileStream stream = new FileStream(pathx, FileMode.Open, FileAccess.Read))
{
pictureEdit1.Image = Image.FromStream(stream);
//stream.Dispose();
}
//alternate2
//Image img = new Bitmap(pathx);
//pictureEdit1.Image = img.GetThumbnailImage(pictureEdit1.Width, pictureEdit1.Height, null, new IntPtr());
//img.Dispose();
}
The documentation on System.Drawing.Bitmap (http://msdn.microsoft.com/en-us/library/0cbhe98f.aspx) says:
The file remains locked until the Bitmap is disposed.
To get around this, you should replace this line:
pictureEdit1.Image = Image.FromFile(pathx);
With this:
Image img = new Bitmap(pathx);
pictureEdit1.Image = img.GetThumbnailImage(pictureEdit1.Width, pictureEdit1.Height, null, new IntPtr());
img.Dispose();
This should load the Bitmap only long enough to create a thumbnail version of the image for use in the PictureBox control, then dispose of it immediately, releasing the lock on the file but still displaying the image on the screen.
Hope this helps!
Edit: Here's the version using using that does the same thing:
using (Image img = new Bitmap(pathx)) {
pictureEdit1.Image = img.GetThumbnailImage(pictureEdit1.Width, pictureEdit1.Height, null, new IntPtr());
}

System Device Location not working

I have code for using the system.device.location api found in windows computers. This should be fairly straightfoward code
var watcher = new GeoCoordinateWatcher();
watcher.PositionChanged +=
new EventHandler<GeoPositionChangedEventArgs<
GeoCoordinate>>(GeoPositionChanged);
watcher.Start();
var coord = watcher.Position.Location;
I mean all I need to do is start a geo watcher and then read the location. But it only ever returns "Location Unknown" and I am wondering if there is an issue with the code, or if something needs to be installed on the computer, or what. I have tried this with a few windows 7 pcs and 1 windows 10 pc and all of them have the share location turned on in the settings. So what is wrong with this code? Also this is the code for the geopositionchanged if that makes any difference.
private static void GeoPositionChanged(object sender,
GeoPositionChangedEventArgs<GeoCoordinate> e)
{
MessageBox.Show("The current location is: " +
e.Position.Location.Latitude + "/" +
e.Position.Location.Longitude + ".");
}
Wait for location services to be ready. Your GeoCoordinateWatcher has an event for status change and another one for position change.
GeoCoordinateWatcher _watcher;
public Class1()
{
_watcher = new GeoCoordinateWatcher();
_watcher.StatusChanged += Watcher_StatusChanged;
_watcher.PositionChanged += GeoPositionChanged;
_watcher.Start();
var coord = _watcher.Position.Location;
}
private void Watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
if (e.Status == GeoPositionStatus.Ready)
{
MessageBox.Show("Watcher is ready. First location: The current location is: " +
_watcher.Position.Location.Latitude + "/" +
_watcher.Position.Location.Longitude + ".");
}
}
private static void GeoPositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
MessageBox.Show("The current location is: " +
e.Position.Location.Latitude + "/" +
e.Position.Location.Longitude + ".");
}

call explore.exe with path from aspx page

I have crate a registry at user system. And i want when user click on my image button that is in my gridview. I shall call url protocol from registry and execute the Explorer.exe with path that i will assign with the image button on the grid.
I create registry that is below
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\PicPath]
#="URL: MPath Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\PicPath\shell]
[HKEY_CLASSES_ROOT\PicPath\shell\open]
[HKEY_CLASSES_ROOT\PicPath\shell\open\command]
#="\"C:\\Windows\\explorer.exe\""
the problem is that when i add
#="\"C:\\Windows\\explorer.exe\ " "%1
%1 is for parameter when i pass c:\Logs my system start to open infinite explorer in taskbar. But when i use #="\"C:\\Windows\\explorer.exe\"" its open explore perfect on client system. But i want that explorer.exe open certain path on client system.
below is my code that i try
protected void grdOrderList_RowDataBound(object sender, GridViewRowEventArgs e)
{
HtmlAnchor a = new HtmlAnchor();
a.HRef = "MPath:OpenForm " + "/root,C:\\Abc";
a.ID = "a1";
System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
img.ID = "img1";
img.Visible = true;
img.ImageUrl = #"~\images\blue_camera.png";
a.Controls.Add(img);
e.Row.Cells[0].Controls.Add(a);
}
So how can i do this . Thanks for you time
protected void grdOrderList_RowDataBound(object sender, GridViewRowEventArgs e)
{
HtmlAnchor a = new HtmlAnchor();
a.HRef = "MPath:OpenForm " + "/root,C:\\Abhishek";
a.ID = "a1";
//System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
//img.ID = "img1";
//img.Visible = true;
//img.ImageUrl = #"~\images\blue_camera.png";
var img = new ImageButton();
img.Click += new ImageClickEventHandler(img_Click);
a.Controls.Add(img);
e.Row.Cells[0].Controls.Add(a);
}
protected void img_Click(object sender, ImageClickEventArgs e)
{
System.Diagnostics.Process.Start(#"c:\blah.txt");
}

'A generic error occurred in GDI+' when saving an image

I have been having quite a problem with this.
Here is my code.
int frame = 0;
//This is a wpf button event
private void up_Click(object sender, RoutedEventArgs e)
{
frame++;
LoadPic();
}
private void LoadPic()
{
string fn = #"C:\Folder\image" + (frame % 2).ToString() + ".png";
Bitmap bmp = new Bitmap(302, 170);
bmp.Save(fn);
bmp.Dispose();
//Picebox is a wpf Image control
Picbox.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri(fn));
}
private void down_Click(object sender, RoutedEventArgs e)
{
frame--;
LoadPic();
}
When I start the program, a wpf window pops open. There are two buttons with the events shown in the code.
When I press the up button twice it works fine. This saves two PNGs to the locations
"C:\Folder\image0.png" and "C:\Folder\image1.png"
The third time I press the button, it should save it to "C:\Folder\image0.png" again.
Instead, it gives the exception 'A generic error occurred in GDI+'.
I have had a similar problem before, and solved it by adding these two lines:
GC.Collect();
GC.WaitForPendingFinalizers();
It didn't work this time.
To avoid the filelock that BitmapImage creates you have to take care of a bit more initialization. According to this question here on SO, it can be done like this (ported to C# from their VB.Net code).
private void LoadPic()
{
string fn = #"C:\Folder\image" + (frame % 2).ToString() + ".png";
Bitmap bmp = new Bitmap(302, 170);
bmp.Save(fn);
bmp.Dispose();
var img = new System.Windows.Media.Imaging.BitmapImage();
img.BeginInit();
img.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
img.UriSource = new Uri(fn);
img.EndInit();
Picbox.Source = img;
}

Where to put label message and only display it after a process is done?

I am a beginner. How and where should i put a label messages after a video is created from below code.
I want to display two messages after the process is completed which is label1: video was created successfully and 2nd messages is videopath of the video.
I only want to display it after the process is completed(video was created).
namespace test
{
public partial class liveRecording : System.Web.UI.Page
{
//video codec
AVIWriter writer = new AVIWriter("MSVC");
protected void Page_Load(object sender, EventArgs e)
{
string streamingSource = "http://xxx.sample.com:85/snapshot.cgi";
string login = "login";
string password = "password";
JPEGStream JPEGSource = new JPEGStream(streamingSource);
JPEGSource.Login = login;
JPEGSource.Password = password;
JPEGSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
JPEGSource.Start();
}
public bool IsRecording = false;
int width = 0;
int height = 0;
Queue<Bitmap> frames = new Queue<Bitmap>(); //Queue that store frames to be written by the recorder thread
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs) //event handler for NewFrame
{
//get frame from JPEGStream source
//Bitmap image = eventArgs.Frame;
Bitmap image = (Bitmap)eventArgs.Frame.Clone(); //get a copy of the Bitmap from the source
width = image.Width;
height = image.Height;
if (IsRecording)
{
//enqueue the current frame to be encoded to a video file
frames.Enqueue((Bitmap)image.Clone());
}
if (!IsRecording)
{
IsRecording = true;
Thread th = new Thread(DoRecording);
th.Start();
}
}
private void DoRecording()
{
//writer.FrameRate = 5;
string SavingPath = (Server.MapPath("~\\video\\"));
string VideoName = "ICS_" + String.Format("{0:yyyyMMdd_hhmmss}", DateTime.Now) + ".avi";
writer.Open(SavingPath + VideoName, width, height);
DateTime start = DateTime.Now;
while (DateTime.Now.Subtract(start).Seconds < 30)
{
if (frames.Count > 0)
{
Bitmap bmp = frames.Dequeue();
writer.AddFrame(bmp);//add frames to AVI file
}
}
writer.Close();//close
}
}
}
I only want to display it after the process is completed(video was created).
Then you should use AJAX, basically. Your "start encoding" (or whatever) request should complete quickly, so the user gets an appropriate page back. That page should contain Javascript to regularly poll the server to see whether the task has completed - you'll need some coordination (e.g. via a randomly generated "job ID" which is provided to the client). You could use something like SignalR for "long polling" (where the AJAX fires a request which is expected to wait until either the job has completed or it times out) or just make a quick polling request every few seconds.
I'm afraid none of this is easy if you're new to web development - but the task you're trying to perform isn't easy, in a world based on HTTP requests and responses.

Categories