how to save image url into session - c#

I want to save a image url into session and this url I get into another page load then load into image button. Is it possible or not? I use the following code to save image url;
Image = Image1.ImageUrl.ToString();
Session["logo"] = Convert.FromBase64String(Image);
But I get the following error, like so;
The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters.
Another page I get this image url,
protected void Page_Load(object sender, EventArgs e)
{
Image img =(Image)Session["logo"];
ImageButton1.Controls.Add(img);
}
The above code also gets an error. So please help me.

Do you want the URL or the binary data of the image? You are storing the URL (e.g. http://www.mycompany.com/logo.png) in the Image variable, but then trying to "decode" it from Base64. It looks like you're trying to get the binary data of the image and store that in session. Why not just store the URL as-is rather than trying to decode it?
string image = Image1.ImageUrl.ToString();
Session["logo"] = image;
protected void Page_Load(object sender, EventArgs e)
{
string imgURL = Session["logo"];
ImageButton1.Controls.Add(new Image() { ImageURL = imgURL });
}
Otherwise you're going to have to create an HttpRequest to capture the binary data of the image, then embed that in the form (which is not as simple as adding an image control since there's nowhere to but the binary data in the web control)

Related

Bind a PictureBox with a string of a url for LoadAsync

Is there a way to bind a PictureBox to a string so that when the string will change, it will call LoadAsync() with the url in the string and load an image?
Currently this is what I have in the auto generated code.
this.itemImagePictureBox.DataBindings.Add(new System.Windows.Forms.Binding("Image", this.MySource, "ItemImage", true));
What I want instead is:
this.itemImagePictureBox.DataBindings.Add(new System.Windows.Forms.Binding("String", this.MySource, "ImageUrl", true));
MySource has a property for url string, and I also tried to make it have a field of an Image, but a Bitmap for example doesn't have a load async feature so I still can't use the url string.
Make sure the WaitOnLoad property is false (default), thus enabling asynchronous image load, and then bind to ImageLocation property:
this.itemImagePictureBox.WaitOnLoad = false;
this.itemImagePictureBox.DataBindings.Add(new System.Windows.Forms.Binding(
"ImageLocation", this.MySource, "ImageUrl", true));
Does it have to be databinding? Why not:
MyImage = new Bitmap(fileToDisplay);
itemImagePictureBox.Image = (Image) MyImage ;
await itemImagePictureBox.LoadAsync();
itemImagePictureBox.Refresh();
Or, if the image is a URL:
Load an image from a url into a PictureBox

asyncFileUpload how to get new File Name

asyncFileUpload how to get a new File Name after upload complete to save new name in database
asp.net c#
I have a form to fill in personal data and Upload own image
Asyncfileupload used to select the picture and in the void AsyncFileUpload_UploadedComplete
Save the file in new name
Now when the user ends of the mobilization of all form and presses the save button to save data , i save every thing in database I can not get the new name to save it.
it is a property in the control. In you event handler check the FileName property. The following code assumes your are checking the file size to to intercept any oversized files.
protected void AsyncFileUpload_UploadedComplete(object sender, AsyncFileUploadEventArgs e)
{
AsyncFileUpload upload = sender as AsyncFileUpload;
if ((upload.PostedFile.ContentLength / 1000) <= 500)
{
string filename = upload.FileName;
// save to the database here
}
}
Hope this helps :-)
Mike

Getting textarea content after setting it with innerHtml

I'm working on something in asp.net that requires allowing the user to change the contents of information within a text area. What happens is that I set the text area contents by getting information within an external .txt, .html, or .rtf file and then set it into the text area using innerHtml.
As for retrieving the data I'm also using innerHtml to do that in the .cs file behind the page. The problem is that when I try doing this, I get back what was previously set even if I had replaced every line of text. For example, if I had originally set "This is the initial text" first then replaced it all with "New text" after, innerHtml will just give me back "This is the initial text".
Is there another way to get the new text or a way to get innerHtml to do what I want?
Edit: Forgot to include code.
On the aspx side of the code, I just have a simple text area,
<textarea id="TextArea1" cols="80" rows="10" runat="server"></textarea>
and on the aspx.cs side,
protected void Page_Load(object sender, EventArgs e)
{
ces = new ContentEditorService.ContentEditorService();
strRtfDir = Server.MapPath("Testfile.rtf");
string strContents = ces.loadEditorContents(strRtfDir);
TextArea1.InnerText = strContents;
}
where ces loads a separate .cs file that does the loading and saving of the text.
As for getting the content, I'm using
protected void Button1_Click(object sender, EventArgs e)
{
string strTxtArea = TextArea1.InnerHtml;
System.Diagnostics.Debug.WriteLine(strTxtArea);
//ces.saveEditorContents(strContents, strRtfDir);
}
which gets the contents and prints them out in the debug window of Visual Studio for now just to see if I managed to get the changed text.
Change your code to this:
if (!IsPostBack)
{
ces = new ContentEditorService.ContentEditorService();
strRtfDir = Server.MapPath("Testfile.rtf");
string strContents = ces.loadEditorContents(strRtfDir);
TextArea1.InnerText = strContents;
}
Because Page_Load will always be excuted even you clicked the Button1.
You can use jquery to get the value inside the text area
var text = $('#yourTextAreaId').html();
or
var text = $('#yourTextAreaId').val();

How do I get the Source of an Image on GestureCompleted

I want to find the URI of an Image on the phone screen when the image is dragged.
I have many images on the screen and I need to know which one has been dragged, I can easily find that from the filename of the image as they are named A.png, B.png and so on.
private void OnGestureCompleted(object sender, Microsoft.Phone.Controls.GestureEventArgs e){
Image image = sender as Image;
TranslateTransform transform = image.RenderTransform as TranslateTransform;
//storing the final values after the gesture is complete
xFin = (e.GetPosition(null).X);
yFin = (e.GetPosition(null).Y);
//failed attempts to convert adress to string
MessageBox.Show(image.Source.ToString());
}
This returns System.Windows.Controls.Image.
and this on the other hand throws an exception stating that ConvertToString hasn't been implemented.
ImageSourceConverter convertor = new ImageSourceConverter();
string location = convertor.ConvertToString(image);
MessageBox.Show(location);
Is there some way this can be done?
If you know that the Source property is BitmapImage (which it probably is if you are passing in a URI to the Image in the Source property in XAML) then you can use UriSource:
MessageBox.Show(((BitmapImage)image.Source).UriSource.ToString());
I suggest you use the Tag property of the Image control to store the info you need though. More scalable robust in the long run.

Add Bitmap to HTML

I have an ASP.NET web form. This web form has an event handler that generates some HTML. This HTML is based on the time-of-day, that is why it is created in the event handler. Based on the time of day, an image is created programmatically via the following method:
private Bitmap GetImageForTime(DateTime time)
{
Bitmap bitmap = new Bitmap();
// Dynamically build the bitmap...
return bitmap;
}
I want to call this method when the HTML is being generated. However, I do NOT want to write the image on the server. Instead, I would like to figure out a way to write it out along with the HTML. In a sense, I'm trying to accomplish the following:
protected void myLiteral_Load(object sender, EventArgs e)
{
string html = "<table><tr><td>";
html += GetImageForTime(DateTime.Now); // This is the problem because it's binary.
html += "</td><td>";
html += GetWeatherHtmlText();
html += "</td></tr></table>";
myLiteral.Text = html;
}
Is this possible? If so, how?
I would suggest implementing an IHttpHandler that generates your image and returns it as a byte stream. Then in the tag on the page, set the src attribute to the address of the HTTP Handler.
<html><body><img src="TimeImageHandler.ashx"/></body></html>
Example: http://www.c-sharpcorner.com/uploadfile/desaijm/httphandlersforimages11152005062705am/httphandlersforimages.aspx
Generic HTTP Handlers are pretty simple to create once you're aware of them:
public class TimeImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
Bitmap bitmap = GetImageForTime(DateTime.Now);
context.Response.ContentType = "image/jpeg";
bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
public bool IsReusable { get { return false; } }
}
The way I have done this is to create an image tag in the HTML and point the image source (<img src="xxx" />) to a page that dynamically creates the image and returns that (and only that) on the response stream, with the correct mime type.
Potentially you could use the data URI scheme in an <img> tag or in some CSS but personally, I wouldn't.
Some more explanation.
Images in HTML are displayed as a result of an <img> element in the HTML. I don't believe there is any other way to display an image in an HTML page.
You will need to write a handler which can be invoked via the URL in the src attribute of the <img> element. That handler will generate and return the image.
Create another aspx page called TimeImage.aspx then in the Page_Load of the code-behind for the page put this code:
void Page_Load(object sender, EventArgs args) {
string mime = "image/jpeg";
Response.ContentType = mime;
Bitmap b = GetImageForTime(DateTime.Now);
var codec = ImageCodecInfo.GetImageEncoders().Where(i => i.MimeType == mime).SingleOrDefault();
if(codec != null)
b.Save(Response.OutputStream, codec, null);
}
Then make your html generation look like this:
protected void myLiteral_Load(object sender, EventArgs e)
{
string html = "<table><tr><td>";
html += "<img src='TimeImage.aspx'>"
html += "</td><td>";
html += GetWeatherHtmlText();
html += "</td></tr></table>";
myLiteral.Text = html;
}
This will create an img tag that calls the TimeImage.aspx page, that page changes the response mime-type to image/jpeg, converts your bitmap to a JPG, and then saves it to the response output stream so that the image tag can display it as a JPG.
If you prefer a different format just change the mime type at the top (e.g. "image/gif" for GIF, or "image/png" for PNG).
Beyond the already given solutions of using the data URI scheme or sending the image dynamically created (so, without storing it), there's another odd solution you can try: a table with each cell 1-pixel tall and wide and proper background per cell, and no borders of course... GIMP e.g. is able to export images this way as html, and if you force the size of cells and table, you can see the image as if it were embedded... of course, it is rather "heavy" if the image is big, but it could work ok for relatively small images.
EDITED (italics part added)

Categories

Resources