ASP.NET image control does not show the image - c#

I am using an image control, but it does not display any image. When I see its viewsource, it displays the image path correct. The URL of the page is devweb.tsgdomain.com/americaneyenew/News.aspx
lblTitle.Text = dtNews.Rows[0]["NewsTitle"].ToString();
string strPhotosImage = dtNews.Rows[0]["Image"].ToString();
if (strPhotosImage != string.Empty)
{
string Extension = strPhotosImage.Substring(strPhotosImage.LastIndexOf("."));
// Server.MapPath("~/ENewsLetterFileUpload/NPH_" + strOriginalFileName + "_1.PDF");
ImgNews.ImageUrl = Server.MapPath("~/ENewsImage/" + iNewsID + "_1" + Extension);
//Server.MapPath("~/ENewsImage/" + iNewsID + "_1" + Extension);
//"../ENewsImage/" + iNewsID + "_1" + Extension;
ImgNews.Visible = true;
}
else
{
ImgNews.Visible = false;
}
Please help me as soon as possible.

you shouldn't use as server.mappath
ImgNews.ImageUrl = "~/ENewsImage/" + iNewsID + "_1" + Extension;
would be enough.

I think you have the wrong URL.
When you do :
String testURL = Server.MapPath("~/ENewsImage/" + iNewsID + "_1" + Extension);
Response.Redirect(testURL);
Is your image there?
Make sure the URL is correct by printing it to the screen or visiting it.

Related

capturing an image from the camera

I am working on a winforms application, the application will attempt to capture an image and save it to the hard drive without the need to present the result or the live stream to the form.
I was using AForge and suddenly I started getting null in my image variable, I tried several other packages and they are all giving the same problem.
I want to know if there is a setting on my system or in my code that is preventing the image from being copied to the variable.
the last one I tried was Microsoft Media Capturing package and the code as follows.
System.Drawing.Imaging.ImageFormat format = null;
format = System.Drawing.Imaging.ImageFormat.Jpeg;
StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
string name = InitialSetup.currentLkr.Name /*+ #"\" + InitialSetup.currentLkr.Name + "_" + DateTime.Now.ToString() + ".jpg"*/;
name = name.Replace(#"/", "_");
name = name.Replace(" ", "_");
name = name.Replace(":", "-");
name = #"D:\" + name;
StorageFolder sf = await ApplicationData.Current.LocalFolder.CreateFolderAsync(name,CreationCollisionOption.OpenIfExists);
await photo.CopyAsync(sf, InitialSetup.currentLkr.Name + "_" + DateTime.Now.ToString() + ".jpg", NameCollisionOption.ReplaceExisting);
await photo.DeleteAsync();
//img.Save(name);
//webcam.Stop();
return name+ InitialSetup.currentLkr.Name + "_" + DateTime.Now.ToString() + ".jpg";

Updating front end during postback

I have a method that runs through a loop that can take quite a while to complete as it requires getting data back form an API.
What I would like to do is display a message on the front end explaining how the system is progressing during each loop. Is there a way to update the front end while processing?
public static void GetScreenshot(List<string> urlList, List<DesiredCapabilities> capabilities, String platform, Literal updateNote)
{
foreach (String url in urlList)
{
String siteName = new Uri(url).Host;
String dir = AppDomain.CurrentDomain.BaseDirectory+ "/Screenshots/" + siteName + "/" + DateTime.Now.ToString("yyyy-MM-dd_HH-mm");
foreach (DesiredCapabilities cap in capabilities)
{
String saveDirectory = "";
if (platform == "btnGenDesktopScreens")
{
saveDirectory = dir + "/" + cap.GetCapability("os") + "-" + cap.GetCapability("os_version") + "-" + cap.GetCapability("browser") + cap.GetCapability("browser_version");
}
else if(platform == "btnMobile")
{
saveDirectory = dir + "/" + cap.GetCapability("platform") + "" + cap.GetCapability("device") + "-" + cap.GetCapability("browserName");
}
updateNote.Text += "<br/>" + cap.GetCapability("platform") + " - " + cap.GetCapability("device") + "-" + cap.GetCapability("browserName");
//I'd like to display a message here
TakeScreenshot(url, cap, saveDirectory);
//I'd like to display a message here
}
}
}
Has anyone come across a method of doing this?
Depending on how you're returning the feedback to the user, you might be able to do this by using HttpResponse.Flush in a loop to push parts of the HTML response to the user a bit at a time. See https://msdn.microsoft.com/en-us/library/system.web.httpresponse.flush(v=vs.100).aspx

File.Delete being used by another process

File delete problem - please help me i cant delete a file .. when i try to delete the i get this massage
c# File.Delete - File being used by another processc# File.Delete -
File being used by another process
protected void Button2_Click1(object sender, EventArgs e)
{
HttpFileCollection hfc = Request.Files;
string x = "";
string foldername = DateTime.Now.ToString().Trim().Replace(" ", "").Replace(":", "").Replace("/", "");
string foldername1 = foldername+"1";
Directory.CreateDirectory(Server.MapPath("~/IMAGEUPLOADCENTER/") + foldername);
Directory.CreateDirectory(Server.MapPath("~/IMAGEUPLOADCENTER/") + foldername1);
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
{
string name =(DateTime.Now.ToString() + i + hpf.FileName).ToString().Trim().Replace(" ", "").Replace(":", "").Replace("/", "");
hpf.SaveAs(Server.MapPath("~/IMAGEUPLOADCENTER/" + foldername + "/") + name);
ResizeImageWidth(497, Server.MapPath("~/IMAGEUPLOADCENTER/" + foldername + "/") + name, Server.MapPath("~/IMAGEUPLOADCENTER/" + foldername1 + "/") + name);
System.Drawing.Image upImage = System.Drawing.Image.FromFile(Server.MapPath("~/IMAGEUPLOADCENTER/" + foldername1 + "/") + name); //System.Drawing.Image.FromStream(FU1.PostedFile.InputStream);
System.Drawing.Image logoImage = System.Drawing.Image.FromFile(Server.MapPath("~/pages002248Xc54/UploadImages/LOGOnew.png"));
using (Graphics g = Graphics.FromImage(upImage))
{
g.DrawImage(logoImage, new Point(upImage.Width - logoImage.Width - 10, 10));
upImage.Save(Server.MapPath("~/IMAGEUPLOADCENTER/" + foldername + "/") + name);
File.Delete(Server.MapPath("~/IMAGEUPLOADCENTER/" + foldername1 + "/") + name);
// Image1.ImageUrl = "~/UploadFiles/2" + "//" + fileName;
}
x = x + "</br><img src='http://hela.co.il/IMAGEUPLOADCENTER/" + foldername + "/" + name + "'/></br>";
}
}
TextArea1.InnerText = x;
FileUpload1.Visible = false;
Button2.Visible = false;
}
Your file is being locked by you and that is preventing you from deleting it. The Image.FromFile method locks the file used to create the object until the Image object is disposed of. So in this case, the image file will remain locked until you dispose of upImage.
Move your File.Delete to after you're done with the image and it will work. In the code below, I added using statements to each variable so that they will get disposed of (and unlock the file), and then I moved the delete statement outside of the using block (after the Image objects are disposed of).
using(System.Drawing.Image upImage = System.Drawing.Image.FromFile(Server.MapPath("~/IMAGEUPLOADCENTER/" + foldername1 + "/") + name))
using(System.Drawing.Image logoImage = System.Drawing.Image.FromFile(Server.MapPath("~/pages002248Xc54/UploadImages/LOGOnew.png")))
using (Graphics g = Graphics.FromImage(upImage))
{
g.DrawImage(logoImage, new Point(upImage.Width - logoImage.Width - 10, 10));
upImage.Save(Server.MapPath("~/IMAGEUPLOADCENTER/" + foldername + "/") + name);
}
File.Delete(Server.MapPath("~/IMAGEUPLOADCENTER/" + foldername1 + "/") + name);
Image.FromFile Method (String)
using statement (C#)

Editing image file name in SQL and deleting actual image if there is modification

I have function in my app to edit the user details in that there is feature which allows us to edit the image in the picturebox.
What I am trying to achieve is if user choose to edit the image to another the app should delete the current image and use the new image and save to db. If users edit other details without touching picturebox then no changes should be made.
I have written below code but but it's not working can correct me or suggest better solution for what I am trying to achieve.
string picPath
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
openFileDialog.Filter = "Image files|*.jpg;*.jpeg;*.png;*.bmp;*.gif";
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
string FileName = openFileDialog.FileName;
profilePicPictureBox.ImageLocation = FileName;
}
if (File.Exists(#"D:\Local Pictures\Users\" + fnameTextBox.Text + "_" + lnameTextBox.Text + "_" + userIdTextBox.Text + ".jpg"))
{
File.Delete(#"D:\Local Pictures\Users\" + fnameTextBox.Text + "_" + lnameTextBox.Text + "_" + userIdTextBox.Text + ".jpg");
picPath = (#"D:\Local Pictures\Users\" + fnameTextBox.Text + "_" + lnameTextBox.Text + "_" + userIdTextBox.Text + ".jpg");
profilePicPictureBox.Image.Save(picPath);
}
when i try to execute the above i am getting a exception on relative path

response redirect makes session_end in global asax

I am getting frustrated due to the session become null.When I click on save button I save the data to DB from code behind and if this successfully I am redirecting the user to the main projects page,using syntax:
Response.Redirect("~/Admin/Projects.aspx?i=esc&prjName=abc",'false');
but its make my session null.Its goes to Globex.asax page ang executes the Session_End and make all session null.
I even tried Server.Transfer but by this the browser url remains same and client doesn't want this.Even some where I read that the Server.Execute is also used to redirect but it is showing some wrong results.
can I use the Response.Redirect without this session null problem ??
(In this page I am creating the text file to store some long description and if this is successful then I am redirecting to the another page.)
Update::
here is my code for button click
protected void lnkbtnAddDescription_Click(object sender, EventArgs e)
{
try
{
if ((!hidProjId.Value.ToString().Equals("") || !hidEditProjId.Value.ToString().Equals("")) && !txtDescription.Value.ToString().Equals(""))
{
//ProjectDescription
int projId = 0;
if (!hidIsEdit.Value.ToString().Equals(""))
{
projId = Convert.ToInt32(hidEditProjId.Value.ToString());
}
else
{
projId = Convert.ToInt32(hidProjId.Value.ToString());
}
ProjectM proj = new ProjectM();
proj.LoadByKey(projId);
string prj = proj.ProjectName.ToString().Replace(" ", "-");
string strDirectoryPath = Server.MapPath("~/ProjectDescription/") + proj.ProjectId + "-" + prj;
if (!Directory.Exists(strDirectoryPath))
{
Directory.CreateDirectory(strDirectoryPath);
string filePath = strDirectoryPath + "/" + proj.ProjectId + "-" + prj + ".txt";
string strDescription = txtDescription.Value.ToString().Replace("<br />", "<p>");
createTextFile(filePath, strDescription);
string dbDirectoryPath = "~/ProjectDescription/" + proj.ProjectId + "-" + prj + "/" + proj.ProjectId + "-" + prj + ".txt";
proj.Description = dbDirectoryPath.ToString();
proj.IsNew = false;
proj.Save();
if (!hidIsEdit.Value.ToString().Equals(""))
{
//Server.Execute("~/SuperAdmin/Projects.aspx?i=esc&prjName=" + proj.ProjectName.ToString() + "",false);
Session["dsProj"] = null;
Session["editProjId"] = null;
Session["fname"] = hidFname.Value.ToString();
Session["UserId"] = hidUserId.Value.ToString();
Session["role"] = hidRole.Value.ToString();
Response.Redirect("~/SuperAdmin/Projects.aspx?i=esc&prjName=" + proj.ProjectName.ToString());
}
else
{
//Server.Execute("~/SuperAdmin/Projects.aspx?i=sc&prjName=" + proj.ProjectName.ToString() + "",false);
Session["dsProj"] = null;
Session["editProjId"] = null;
Session["fname"] = hidFname.Value.ToString();
Session["UserId"] = hidUserId.Value.ToString();
Session["role"] = hidRole.Value.ToString();
Response.Redirect("~/SuperAdmin/Projects.aspx?i=sc&prjName=" + proj.ProjectName.ToString());
}
}
else
{
Directory.Delete(strDirectoryPath, true);
Directory.CreateDirectory(strDirectoryPath);
string fileName = proj.ProjectName.ToString().Replace(" ", "-");
string filePath = strDirectoryPath + "/" + proj.ProjectId + "-" + fileName + ".txt";
string strDescription = txtDescription.Value.ToString().Replace("<br>", "<p>");
createTextFile(filePath, strDescription);
string dbDirectoryPath = "~/ProjectDescription/" + proj.ProjectId + "-" + proj.ProjectName.ToString() + "/" + proj.ProjectId + "-" + proj.ProjectName.ToString() + ".txt";
proj.Description = dbDirectoryPath.ToString();
proj.IsNew = false;
proj.Save();
if (!hidIsEdit.Value.ToString().Equals(""))
{
//Server.Execute("~/SuperAdmin/Projects.aspx?i=esc&prjName=" + proj.ProjectName.ToString() + "", false);
Session["dsProj"] = null;
Session["editProjId"] = null;
Session["fname"] = hidFname.Value.ToString();
Session["UserId"] = hidUserId.Value.ToString();
Session["role"] = hidRole.Value.ToString();
Response.Redirect("~/SuperAdmin/Projects.aspx?i=esc&prjName=" + proj.ProjectName.ToString());
}
else
{
//Server.Execute("~/SuperAdmin/Projects.aspx?i=sc&prjName=" + proj.ProjectName.ToString() + "", false);
Session["dsProj"] = null;
Session["editProjId"] = null;
Session["fname"] = hidFname.Value.ToString();
Session["UserId"] = hidUserId.Value.ToString();
Session["role"] = hidRole.Value.ToString();
Response.Redirect("~/SuperAdmin/Projects.aspx?i=sc&prjName=" + proj.ProjectName.ToString());
}
}
}
}
catch (Exception)
{
}
}
private void createTextFile(string filePath, string strDescription)
{
try
{
StreamWriter w = File.CreateText(filePath);
w.WriteLine(strDescription);
w.Flush();
w.Close();
}
catch (Exception ex)
{
}
}
Before you redirect to the next page,
assign the existing session value in the button click event also.
This would solve the problem.
hope this will be helpful to you,
ASP.NET Session becomes null after postback on local
This think your problem is related to some kind of permission denial, which is nulling your session.
There must be a problem in creating or saving the text file.
This may be making Session_End call so you are getting all values null.
Folder delete, move, rename causes Session end and long refresh
http://www.telerik.com/community/forums/aspnet-ajax/file-explorer/folder-delete-move-rename-causes-session-end-and-long-refresh.aspx#1365780

Categories