Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a picture on the server is a generous certificate of appreciation when the student searches with a number the student's data is written on the image and without saving it on the server the student protects it on his machine
I got the code to write on the image, but it saves it on the server. Please help me. Instead of saving on the server, the downloader works for the student.
This is the writing code

<pre>Bitmap bitMapImage = new Bitmap(Server.MapPath("img1.jpg"));
            Graphics graphicImage = Graphics.FromImage(bitMapImage);
            graphicImage.DrawString("testing 1 2 3",
            new Font("Arial", 20, FontStyle.Bold),
            SystemBrushes.WindowText, new Point(0, 0));
                    //  I want to replace this by downloading it to the student machine //instead of saving it to the server          bitMapImage.Save(Server.MapPath("~/Images")+"newImage.jpg" , ImageFormat.Jpeg);
            graphicImage.Dispose();

bitMapImage.Dispose();

What I have tried:

i
tried

download image to client pc
Posted
Updated 25-Jun-20 1:08am
Comments
Richard MacCutchan 25-Jun-20 7:00am    
You cannot download files to the user's PC, it would be a major security breach. Only they can do it by requesting a download. What you must do is to create a file which they can then download through their browser.

Convert the image to a byte array, then use BinaryWrite to send it to the client as a file:
C#
Response.AddHeader("Cache-Control", "no-cache, must-revalidate, post-check=0, pre-check=0");
Response.AddHeader("Pragma", "no-cache");
Response.AddHeader("Content-Description", "File Download");
Response.AddHeader("Content-Type", "application/force-download");
Response.AddHeader("Content-Transfer-Encoding", "binary\n");
Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
Response.BinaryWrite(data);
Response.End();
 
Share this answer
 
Comments
Richard MacCutchan 25-Jun-20 7:36am    
Dumb question but ... can the server force that without the user's intervention?
OriginalGriff 25-Jun-20 7:58am    
Yes.
It's up to the user what happens to it though, and Chrome at least will warn you about zip, exe, and suchlike.
Richard MacCutchan 25-Jun-20 8:15am    
Thanks, wasn't aware of that.
1. Create a MemoryStream and "save" the image to that.
2. Write the above MemoryStream as the response

Will look something like this
C#
Bitmap bitMapImage = new Bitmap(Server.MapPath("img1.jpg"));
Graphics graphicImage = Graphics.FromImage(bitMapImage);
graphicImage.DrawString("testing 1 2 3",
new Font("Arial", 20, FontStyle.Bold),
SystemBrushes.WindowText, new Point(0, 0));
        
// I want to replace this by downloading it to the student machine 
// instead of saving it to the server
// bitMapImage.Save(Server.MapPath("~/Images")+"newImage.jpg" , ImageFormat.Jpeg);

MemoryStream memoryStream = new MemoryStream();
bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);

byte[] bytesInStream = memoryStream.ToArray();
memoryStream.Close(); 

Response.Clear();
Response.ContentType = "image/jpeg";
Response.AddHeader("content-disposition", "attachment; newImage.jpg");
Response.BinaryWrite(bytesInStream);

graphicImage.Dispose();
bitMapImage.Dispose();

Response.End();
Helpful info:
c# - Saving a bitmap into a MemoryStream - Stack Overflow[^]
c# - How to download memorystream to a file? - Stack Overflow[^]
 
Share this answer
 
Comments
Saeeed Sayed 25-Jun-20 7:49am    
I tried it but it doesn't download the image but it download the page by name with ex aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900