I am using canvas and image controls to allow a user to create a signature image and display it and repeat until they are satisfied with their signature. In the code the save process simply displays the signature in an image control named saveSignature.
I would like to actually save the image control image to a .png file in the remote website folder for that customer (e.g. ~/photos/Jim Smith/). What do I have to do to also save it to a png file on the webserver? Below is the relevant html and JavaScript.
[WebMethod]
public static void SaveImage(string imageData)
{
string fileNameWitPath = HostingEnvironment.ApplicationPhysicalPath+"/files/" + DateTime.Now.ToString().Replace("/", "-").Replace(" ", "- ").Replace(":", "") + ".png";
using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(imageData);
bw.Write(data);
bw.Close();
}
}
}
Below is the result of this demo:
But i don't really think it is a good idea to save the image each time the user "preview" his signature, add another button for "save" might be better.
It looks like I need a new page called SaveImageDemo.aspx, is that correct?
No, no necessary to create a new page.
dlchase
Can you explain the URL in the ajax call?
The url in the ajax call points to the webmethod(SaveImage in this case) in back code of specific page(SaveImageDemo.aspx for example).
I guess your project is a webform one since you are using asp.net contols, the standard format for url of ajax call in a
webform project is url: "{page.aspx}/{webmethod}".
In my demo, the current page name IS SaveImageDemo(you can find this by the url of browser in my gif) and the webmethod IS SaveImage. So you will need to change the page name and webmethod name to fit your project.
For more information about how to use JQuery Ajax in a webform project, you can refer to this
article.
In addition, for MVC project, the url format should be url: "{Controller}/{Action}". Please feel free to contact if have any further concern.
Can you explain the URL in the ajax call? It looks like I need a new page called SaveImageDemo.aspx, is that correct? Please reply. Thanks.
You will need to create web method in your .aspx.cs file in order to perform AJAX call. Refer
this article for codes.
Helping you always. Don't forget to click "Mark as Answer" on the post that helped you.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
I just discovered that we have to do a full Postback on our page because we need some additional code to handle when saving the image to a file.
Below is the page markup where the asp.net Image control resides. I need to save the image that I am loading into the saveSignature Image control. I have used code to save from an image file but not from a control. I have the save from a file path code
that I have used before shown below the page markup. Thanks.
<asp:Panel ID="PanelSignature" runat="server" CssClass="hide" ClientIDMode="Static">
<div id="canvas">
<canvas class="" id="newSignature"
style="position: relative; margin: 0; padding: 0; border: 1px solid #c4caac;">
</canvas>
</div>
<script type="text/javascript">
signatureCapture();
</script>
<asp:Button ID="BtnSaveSignature" runat="server" Text="Save signature" OnClientClick="return signatureSave();" />
<asp:Button ID="BtnClearSignature" runat="server" Text="Clear signature" OnClientClick="return signatureClear();" />
<br />
Saved Image
<br />
<asp:Image ID="saveSignature" runat="server" ClientIDMode="Static" />
</asp:Panel>
Dim bmpImg As Bitmap = Nothing
Using fileStream As FileStream = File.OpenRead(strFilePath)
Dim memStream As New MemoryStream()
memStream.SetLength(fileStream.Length)
fileStream.Read(memStream.GetBuffer(), 0, CInt(fileStream.Length))
bmpImg = FilesClass.Resize_Image(fileStream, 2533, 1900)
bmpImg.Save(filePath, ImageFormat.Jpeg)
fileStream.Close()
fileStream.Dispose()
End Using
Member
350 Points
1528 Posts
Save img to file from canvas
Jun 15, 2020 03:06 PM|dlchase|LINK
I am using canvas and image controls to allow a user to create a signature image and display it and repeat until they are satisfied with their signature. In the code the save process simply displays the signature in an image control named saveSignature. I would like to actually save the image control image to a .png file in the remote website folder for that customer (e.g. ~/photos/Jim Smith/). What do I have to do to also save it to a png file on the webserver? Below is the relevant html and JavaScript.
Contributor
3140 Points
983 Posts
Re: Save img to file from canvas
Jun 16, 2020 03:41 AM|Yang Shen|LINK
Hi dlchase,
You can use JQuery Ajax and Webmethod to achieve your requirement. Please refer to below code:
aspx:
cs:
Below is the result of this demo:
But i don't really think it is a good idea to save the image each time the user "preview" his signature, add another button for "save" might be better.
Best Regard,
Yang Shen
Member
350 Points
1528 Posts
Re: Save img to file from canvas
Jun 16, 2020 03:02 PM|dlchase|LINK
Can you explain the URL in the ajax call? It looks like I need a new page called SaveImageDemo.aspx, is that correct? Please reply. Thanks.
Contributor
3140 Points
983 Posts
Re: Save img to file from canvas
Jun 17, 2020 01:30 AM|Yang Shen|LINK
Hi dlchase,
No, no necessary to create a new page.
The url in the ajax call points to the webmethod(SaveImage in this case) in back code of specific page(SaveImageDemo.aspx for example).
I guess your project is a webform one since you are using asp.net contols, the standard format for url of ajax call in a webform project is
url: "{page.aspx}/{webmethod}"
.In my demo, the current page name IS SaveImageDemo(you can find this by the url of browser in my gif) and the webmethod IS SaveImage. So you will need to change the page name and webmethod name to fit your project.
For more information about how to use JQuery Ajax in a webform project, you can refer to this article.
In addition, for MVC project, the url format should be
url: "{Controller}/{Action}"
. Please feel free to contact if have any further concern.Thanks,
Yang Shen
Participant
1253 Points
926 Posts
Re: Save img to file from canvas
Jun 17, 2020 03:13 AM|yogyogi|LINK
You will need to create web method in your .aspx.cs file in order to perform AJAX call. Refer this article for codes.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
Member
350 Points
1528 Posts
Re: Save img to file from canvas
Jun 23, 2020 05:31 PM|dlchase|LINK
I just discovered that we have to do a full Postback on our page because we need some additional code to handle when saving the image to a file.
Below is the page markup where the asp.net Image control resides. I need to save the image that I am loading into the saveSignature Image control. I have used code to save from an image file but not from a control. I have the save from a file path code that I have used before shown below the page markup. Thanks.