Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reference a Image

Status
Not open for further replies.

liamba

Technical User
Jan 6, 2007
21
IE
Hi,
the code below draws a line graph by getting data from a sql server database,draw the line and saving it to my C:. However i want to be able to view this graph on a image, however i cant get the image to display since im running it on my local host and the local host cant reference anything outside the project folder i believe. i think i need to save the graph to this folder and reference it using server.mapPath although im not to sure.

any insight would be great
Thanks


protected void Draw()
{
for (int intRowCounter = 0; intRowCounter <= objDataTable.Rows.Count - 1; intRowCounter++)
{
DataRow drowRow = objDataTable.Rows[intRowCounter];
objGraphics.DrawLine(Pens.Red, PrevX, PrevY, intRowCounter * XScale, ImageHeight - (float.Parse(drowRow.ItemArray.GetValue(43).ToString()) * float.Parse(YScale.ToString())));
PrevX = intRowCounter * XScale;
PrevY = ImageHeight - (Int32.Parse(drowRow.ItemArray.GetValue(43).ToString()) * Int32.Parse(YScale.ToString()));
}

objBitmap.Save("C:\\img1.gif"); <----here i believe is where i need to save and reference the image
Image4.ImageUrl = "C:\\img1.gif";
}
 
You should not have paths like C:\\img1.gif. When you deploy the webapp, it will creash. Use instead relative paths, using the Server.mappath as you said. To save the image to the root (for example) write: Server.MapPath("~") + "..."

When you deploy it.. you will not have access to the user's C: drive to save the image. I know that you intend to save it to the server, but this code will try to save it to the user's machine ---> Exception.

To load the image, i think that it would be better to load it not from the server, but from a stream object. I have not tested this, but try something like: (There are many possibilities that it will.... not work :)

this.Image4.ImageUrl = System.Drawing.Image.FromStream(objGraphics);
 
I understand what you are saying but my lack of programming ability is holding me back. i am still having trouble saving the graph, i have created a folder "LineGraphs" to store the graph but am having difficulty with this.i have attched the code below.

any help would be great.

protected void Draw()
{
for (int intRowCounter = 0; intRowCounter <= objDataTable.Rows.Count - 1; intRowCounter++)
{
DataRow drowRow = objDataTable.Rows[intRowCounter];
objGraphics.DrawLine(Pens.Red, PrevX, PrevY, intRowCounter * XScale, ImageHeight - (float.Parse(drowRow.ItemArray.GetValue(43).ToString()) * float.Parse(YScale.ToString())));
PrevX = intRowCounter * XScale;
PrevY = ImageHeight - (Int32.Parse(drowRow.ItemArray.GetValue(43).ToString()) * Int32.Parse(YScale.ToString()));
}
string LineGraphPath;
string Member = Session["MemberID"].ToString();
string FileName = "_img1.gif";
objGraphics.Save(LineGraphPath = Server.MapPath("LineGraphs") + "\\" + (Member + FileName));
//Server.MapPath("~") + "/All/img1.gif ";
//Image4.ImageUrl = objBitmap.ToString();
this.Image4.ImageUrl = System.Drawing.Image.FromStream(objGraphics);
//string LineGraphPath = Server.MapPath("LineGraphs") + "\\" + (Member + FileName);
}
 
Also, show the line that you declared the objGraphics
 
the objGraphics is declared as follows
Graphics objGraphics;

and called in the page load
InitaliseGraphics();

which calls
protected void InitaliseGraphics()
{
objGraphics = Graphics.FromImage(objBitmap);
}

following this the Draw() is called.

there are various errors that i am getting such
1.No overload for method 'Save' takes '1' arguments
2.The best overloaded method match for 'System.Drawing.Image.FromStream(System.IO.Stream)' has some invalid arguments
3.Argument '1': cannot convert from 'System.Drawing.Graphics' to 'System.IO.Stream'
 
The errors 2 and 3 are from my code.
The 1st says tha the .Save() wants more than one arguments... and you have only one.

I'll have a look at it and see if i find something.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top