When I try this I get an error message that the Response.--- type isn't supported. That this cs file has got nothing to do with HTML.
I think I need to make it so that I am in a Page_load method, but that I want to be sure that this cannot work this way. The cs file needs to be linked to a html page. I thought via the Page.cshtml file.
objBitmap = new Bitmap(300, 300);
objGraphic = Graphics.FromImage(objBitmap);
Font font = new Font("Impact", 20, FontStyle.Regular);
objGraphic.DrawString("Test", font, Brushes.Blue, 10, 5);
Response.ContentType = "image/jpeg";
Response.AddHeader("content-disposition", "inline; filename=test.jpg");
// Render the image to the HTML output stream.
objBitmap.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
1. your "name" attributes on the input elements do not match your Request[]. (input name="zoom" can be captured by using Request["zoom"])
2. all inputs from form post method return as strings even if the users typed numbers.
3. there is no need to set the initial var (zoom and startRe) to 0.0f. You can leave these null (for more control down the stack).
4. my alteration to your code isnt the best - but it does follow some convention. (i would still check to see if the newZoom and newStartRe are in fact floats now - another if)
5. you should pay attention to what the user inputs as well. Maybe some client side validation? We don't want letters converted to floats... it'll blow up
6. we can use the error var anywhere on our page to display the message. try this: @if(!error.IsEmpty){<span class="errorClass">@error</span>}
I hope that helps... I didnt check the outcome but it should work.
virtual pilo...
Member
28 Points
47 Posts
How do I do this?
Oct 21, 2010 10:39 AM|LINK
When I try this I get an error message that the Response.--- type isn't supported. That this cs file has got nothing to do with HTML.
I think I need to make it so that I am in a Page_load method, but that I want to be sure that this cannot work this way. The cs file needs to be linked to a html page. I thought via the Page.cshtml file.
default.cshtml
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<img src=@Href("Page.cshtml") />
</body>
</html>
Page.cshtml
@using System.Drawing;
@{
var picture = new ProduceImageFile(1.0f, 1.0f, 1.0f, 100);
}
ProduceImageFile.cs
public ProduceImageFile(float zoomIN, float startReIN, float startImIN, int startColorIN)
{
zoom = zoomIN;
startRe = startReIN;
startIm = startImIN;
startColor = startColorIN;
objBitmap = new Bitmap(300, 300);
objGraphic = Graphics.FromImage(objBitmap);
Font font = new Font("Impact", 20, FontStyle.Regular);
objGraphic.DrawString("Test", font, Brushes.Blue, 10, 5);
Response.ContentType = "image/jpeg";
Response.AddHeader("content-disposition", "inline; filename=test.jpg");
// Render the image to the HTML output stream.
objBitmap.Save(Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Jpeg);
objGraphic.Dispose();
objBitmap.Dispose();
myPen.Dispose();
}
.cs HTML c#
shakesperoo2
Member
20 Points
16 Posts
Re: How do I do this?
Oct 21, 2010 11:54 AM|LINK
change the path and always clrear the response
and end it
use
Response.Clear()
and
Response.End();
i think using this will be fair enough
Mark As Answer
virtual pilo...
Member
28 Points
47 Posts
Re: How do I do this?
Oct 21, 2010 01:59 PM|LINK
I solved my problem by coding as Razor, but now I ran into another problem... Why doesn't this work? This should be so easy!
form to enter numbers
<form method="post" action="">
<fieldset>
<legend>Enter parameters</legend>
<div>
<label for="zoom">Range:</label>
<input type="text" name="Range" value="@Request["zoom"]" />
</div>
<div>
<label for="startRe">Start Real:</label>
<input type="text" name="StartReal" value="@Request["startRe"]" />
</div>
The Razor code to assign the input
var zoom = 0.0f;
var startRe = 0.0f;
if (IsPost) {
var errors = false;
if (Request["zoom"].IsFloat()) {
zoom = Request["zoom"].AsFloat();
}
else {
errors = true;
@:Range - floating point number required.<br />
}
if (Request["startRe"].IsFloat()) {
startRe = Request["startRe"].AsFloat();
}
else
{
errors = true;
@:Start Real - floating point number required.<br />
}
This doesn't work, I get errors and I wouldn't have a clue why this doesn't work. I get into the else part instead of the then part.
RAZOR
Erik5388
Member
81 Points
84 Posts
Re: How do I do this?
Oct 21, 2010 02:53 PM|LINK
<form method="post"> <fieldset> <legend>Enter parameters</legend> <div> <label for="zoom">Range:</label> <input type="text" id="zoom" name="Range" value="@Request["Range"]" /> </div> <div> <label for="startRe">Start Real:</label> <input type="text" id="startRe" name="StartReal" value="@Request["StartReal"]" /> </div> </fieldset> </form> @{ var zoom = ""; var startRe = ""; var error = ""; if(IsPost){ zoom = Request["Range"]; startRe = Request["StartReal"]; float newZoom = 0; float newStartRe = 0; if(zoom.IsEmpty && startRe.IsEmpty){ error = "Either your range or start real are empty. Try again!"; } else{ newZoom = zoom.AsFloat(); newStart = startRe.AsFloat(); } } }A couple things here...
1. your "name" attributes on the input elements do not match your Request[]. (input name="zoom" can be captured by using Request["zoom"])
2. all inputs from form post method return as strings even if the users typed numbers.
3. there is no need to set the initial var (zoom and startRe) to 0.0f. You can leave these null (for more control down the stack).
4. my alteration to your code isnt the best - but it does follow some convention. (i would still check to see if the newZoom and newStartRe are in fact floats now - another if)
5. you should pay attention to what the user inputs as well. Maybe some client side validation? We don't want letters converted to floats... it'll blow up
6. we can use the error var anywhere on our page to display the message. try this: @if(!error.IsEmpty){<span class="errorClass">@error</span>}
I hope that helps... I didnt check the outcome but it should work.
Erik
zettersten.com
client side dev
canvas/html5/js/css
virtual pilo...
Member
28 Points
47 Posts
Re: How do I do this?
Oct 21, 2010 04:02 PM|LINK
@{
var stringvar = "3.14";
var floatvar = stringvar.AsFloat();
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
@floatvar
Answer = 0!
</body>
</html>
Why doesn't this work?
RAZOR
virtual pilo...
Member
28 Points
47 Posts
Re: How do I do this?
Oct 21, 2010 05:06 PM|LINK
Solution:
NumberFormatInfo formatInfo = new NumberFormatInfo();
formatInfo.NumberDecimalSeparator = ".";
if (IsPost) {
var errors = false;
var zoomstring = Request["Range"];
if (zoomstring.IsFloat()) {
zoom = float.Parse(zoomstring, formatInfo);
}
else {
errors = true;
@:Range - floating point number required.<br />
}
RAZOR
Erik5388
Member
81 Points
84 Posts
Re: How do I do this?
Oct 21, 2010 06:39 PM|LINK
why are you doing var floatvar?
You should cast it as float floatvar = stringvar.AsFloat();
then print @floatvar
zettersten.com
client side dev
canvas/html5/js/css