I'm trying to add a bitmap to a json string I'm sending to a website.
To do this I'm trying to convert the bitmap into a base64string, but this string turns up to be empty if checked on the website.
This is the code I use to covert the bitmap into a base64string
Bitmap bmp = (Bitmap)Image.FromFile(#"C:\ProgramData\test.jpg", true);
System.IO.MemoryStream mStream = new System.IO.MemoryStream();
bmp.Save(mStream,System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageBytes = mStream.ToArray();
string base64String = Convert.ToBase64String(imageBytes);
but base64String seems to be empty, yet if I make a bitmap out of it again and save that bitmap to my PC everything seems to be fine.
To do this I'm trying to convert the bitmap into a base64string, but
this string turns up to be empty if checked on the website.
Your code is working when running from console application (btw look at comments you can do it more easily). Problem most likely is that local file system outside your web application is not accessible by web application. Try to read file from your web application folder.
To read file outside of your application folder look at this answer ASP.NET - Reading and writing to the file-system, outside the application The easiest way is to impersonate your AppPool with user account with appropriate rights.
Related
I have an application that will save items in the DB as a blob. Users have been using it to save emails (.msg files).
The app also allows users to view uploaded files. The app gets the file as a byte array and displays it.
However it is failing when trying to open .msg files.
It used to work using the following:
Response.Body.WriteAsync(bytes, 0, bytes.Length);
However that has stopped working recently and now just downloads the file to their PC instead of opening it.
I have tried:
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(imageModel.imageString); // the string that gets converted to a byte array
System.Diagnostics.Process.Start(startInfo);
But I get an error saying the string is too long.
Does anyone know how I can open a byte array in the default email application? It will be outlook for these uers. I'd prefer to not first save it as a file on their PC and send the byte array directly to outlook.
The workaround I used for this it to simple have the app download the .msg file to the users comoputer, then open in the default mail app.
I was not able to get it to open in Outlook from a byte array.
I am using .NET Core 3.0 MVC app with a file upload for users. These files assumed to be an image, but for obvious reasons, I validate the image before saving it to the server.
Now I have ran into an issue, when a user tries to upload a JPEG image (which seems to be fine as he had to email it to me for further inspection) and this image seems to be a WebP image or was converted from a WebP Image.
The issue is that one of the check for each image is
using (var bitmap = new Bitmap(postedFile.OpenReadStream()))
{
}
This threw an exception "Parameter is not valid" and because of this, validation fails.
If I read the buffer of the uploaded IFromFile, it starts with RIFFÚ¶\0\0WEBPVP8
This is how I knew, this is not a normal image.
Any way to decode or validate this image as any other?
I have a bitmap or lets call it a file that I want to upload to my c# server, this bitmap (file) is captured via camera and saved into the sd card, but I have to no idea how exactly should I send it to my c# server. I don't know exactly what should I send in my request and what should be my server function's parameter to receive the file. Any help would be appreciated.
I've found this which shows how to send a file to server but it's using a php server side coding so I couldn't completely understand the process.
I'm not asking for code or anything, just a direction or explanation so I understand the concept. Thanks in advance
You need to do the following
Create a api call that will accept the file and store that file somewhere on the server.
You need to upload the image to the server with the help of the api call by passing the image file in it
Uploading image to server example
How do I send a file in Android from a mobile device to server using http?
Refer this for uploading with progress bar
Doesn't need to upload the images, just convert Image into Base64 an then upload this string to the specific field.
You can easily upload and retrieve images.
Before pushing it to server, convert your bitmap to string and push it using api. and the db field should be BLOB.after uploading the string should be converted back to bitmap and then image(This conversion should be written by api guy).
public String BitMapToString(Bitmap bitmap) {
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] b=baos.toByteArray();
String temp=Base64.encodeToString(b, Base64.DEFAULT);
return temp;
}
I need to save a Bitmap as a jpeg file on my website which is hosted on Hostgator, via WCF service.
Code that needs to save file looks like this
Bitmap bmPhoto = ...
var path = "~/httpdocs/Images/" + filename + ".jpg";
bmPhoto.Save(HostingEnvironment.MapPath(path), ImageFormat.Jpeg);
On my PC I'm using Server.MapPath and it works perfectly well. But I can't use Server.MapPath in WFC service. Instead I use HostingEnvironment.MapPath in System.Web.Hosting and it doesn't work. What do I need to do to get it to work?
I'm having an issue downloading a file. I'm running this website on my local IIS. The BaseUrl correctly has the address of my local IIS site. The moduleImgPath is a Sitecore media item: "/sitecore/shell/~/media/Racking/module-image.png". The BaseUrl has the structure "http://local-$company.com".
The code used for the download is essentiall shown below. The method errors on Image.FromStream() with a System.ArguementException - "Parameter is not valid."
Stream stream = new MemoryStream(new WebClient().DownloadData(RackingConfigHelper.BaseUrl + moduleImgPath));
Image objImage = Image.FromStream(stream);
My question essentially revolves around - can you use a WebClient this way, to download data from what essentially a local source? Or will I need to deploy this code to my test environment to test it out? If I can, do I need to worry about ports?
It looks like your image data is not a jpg (I think this is typically what is expected for Image).
You can see why the error happens on this msdn link
and you can see the docs for Image on this msdn link
I would hazard a guess you are either trying to use a non-jpg OR perhaps the jpg you have may be unusually formed.