0

I am new to c# and asp.net. I am trying to save an image in a default location without giving the path. Currently I dont know how to do this.

This is my function:

[HttpGet]
        [Route("GetImages")]
        public async Task<IHttpActionResult> GetImagesAsync()
        {
           
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            WebProxy myproxy = new WebProxy("corpproxy1.tatasteel.com", 80);
            myproxy.BypassProxyOnLocal = false;
            //myproxy.UseDefaultCredentials = true;


            HttpClientHandler handler = new HttpClientHandler()
            {
                Proxy = myproxy
            };
            using (var client = new HttpClient(handler))
            {
                var bytes =  await 
                client.GetByteArrayAsync("https://firebasestorage.googleapis.com/v0/b/tsl- 
                 coil-qlty-monitoring-dev.appspot.com/o/1a60ce3b-eddf-4e72-b2af-b6e99873e926? 
                 alt=media&token=61399a02-1009-4bb9-ad89-d1235df900e4");
                var bytes_image = Convert.ToBase64String(bytes);

                byte[] bitmap = bytes;

                using (Image image = Image.FromStream(new MemoryStream(bitmap)))
                {

                    

                    image.Save("D:\\CQMS_Images_JPEG\\output.png", ImageFormat.Png);  // Or 
                      Png

                    return Ok(image);
                }

               
          }

I do not want to give a path like this

image.Save("D:\\CQMS_Images_JPEG\\output.png", ImageFormat.Png); 

I want it to store in a default location maybe me just giving this path name as in:

Or store it in the root folder. This Application will be used for mobiles as well hence the requirement

           image.Save("output.png", ImageFormat.Png);

How do I do this Please help??

sambit
  • 256
  • 2
  • 18
  • And why do you want to save it anyway? – Klamsi Jun 09 '21 at 07:43
  • dont I have to save it in order to get them in my computer? – sambit Jun 09 '21 at 07:44
  • You have a method that returns an image object. It does not matter if this image is additionally saved on disk or not. – Klamsi Jun 09 '21 at 07:50
  • I have to save it my device. It has to reside somewhere in my device otherwise how do I access it? – sambit Jun 09 '21 at 07:52
  • Basically how do I see this image in my device? – sambit Jun 09 '21 at 07:54
  • Looks like you have web client running on mobile app and whant to save image downloaded from a web service. You may try to check the [Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);](https://stackoverflow.com/a/7908446/940182) method – oleksa Jun 09 '21 at 08:01
  • Uh no it will run on mobile as well as computer – sambit Jun 09 '21 at 08:04
  • I'm not sure. Maybe you mix something up. You are here on the server side. Your client (I think what you mean with "device") will never have any chance to access the servers hard disk. – Klamsi Jun 09 '21 at 08:07
  • Image.save saves the image in my device whether laptop or mobile in the specified location. I want that location to be the default location or the root folder so that it works on both mobile and computer. – sambit Jun 09 '21 at 08:11
  • 1. You shouldn't use the `System.Drawing` image manipulation classes in an asp environment at all. They rely on GDI+ [which isn't safe to use in web environments](https://learn.microsoft.com/en-us/dotnet/api/system.drawing?view=net-5.0#remarks). 2. This will just save it on the server, not on the user's system. – Nyerguds Jun 25 '21 at 12:29

1 Answers1

0

If your application is running in a Client Environment you can use something like this: (But I have my doubt you are on a Server how your method looks like. In that case you cannot decide the path of saving as is the browser to do that, might allows you to chose or save directly in a path)

            using (Image image = Image.FromStream(new System.IO.MemoryStream(bitmap)))
            {
                image.Save(Application.LocalUserAppDataPath + "\\output.png", ImageFormat.Png);
            }
G3nt_M3caj
  • 2,497
  • 1
  • 14
  • 16
  • for using this Application.LocalUserAppDataPath do I need to add anything? – sambit Jun 09 '21 at 08:29
  • No, you don't if your application is a desktop based app – G3nt_M3caj Jun 09 '21 at 08:33
  • it is showing error for application. The name does not exist in the current context – sambit Jun 09 '21 at 08:34
  • this is a web based app – sambit Jun 09 '21 at 08:36
  • Yes, as my doubts up becomes out. You are in a ASP.NET app. Then there is the browser decides where you can save – G3nt_M3caj Jun 09 '21 at 08:37
  • yes but I am giving the path right I am deciding where it is saved If I remove the path and just do output.jpeg it will show a 500 internal error on the client side – sambit Jun 09 '21 at 08:40
  • 1
    Yes but it's save on server. If you are in local desk (under development of this web-based app) your local machine behaves as a Server. As you see “localhost://blabla”. If a day this app will be hosted on a hosting provider you cannot save on devices. Is the browser that can do that – G3nt_M3caj Jun 09 '21 at 08:46
  • But in this case you can only "download" it. – G3nt_M3caj Jun 09 '21 at 08:47