Is it possible to read WebP images into a C# Bitmap object? - c#

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?

Related

How to load Dicom image from server (path contains https://)

How to load Dicom image from server (path contains https://)
Am using fo-Diocm library.
var image = new DicomImage(filePath);
if filePath is from local directory its working fine.
if filePath is from server (like https://example.com/filepath.dcm), its throwing 'Dicom.DicomFileException' saying that specified path value is not correct.
What is the right way to load DicomImage from server ??
AFAIK: Not at all. Obtaining DICOM Files through Web API is referred to as WADO (Web Access to DICOM Objects), and there is an open feature request in fo-dicom to support this, but it has been assigned quite a high priority.
However, the URL https://example.com/filepath.dcm does not adhere to WADO encoding, so maybe you cannot use a DICOM communication protocol to obtain the image (or you do not want to). But in this case, your task is simply "downloading a file from an URL" and not related to DICOM at all. After the download is completed, you have it stored locally, and can go on with local file access as you are used to.
The "missing link": How to download a file from an URL

androdi upload bitmap to c# server from device

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;
}

Converting bitmap to base64string

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.

Sending image (.jpg) byte data to an iOS device from a .NET web service, then on the iOS device, encoding it to its respective file type (.jpg)

I have a web service that sends image byte data (.jpg) to an iOS device. The iOS device saves that data to the .jpg that it is, and once it's saved to the local file system, the file cannot be opened due to a "corrupt file" message we get when trying to open it on the iOS device. The specifics of this message are irrelevant.
I am a .NET developer developing the web service. I don't know anything about Objective C - we have another developer handling that.
QUESTION: What I need to know is if the iOS device expects the byte data to be encoded a certain way, whether it has to be a Base64 string, etc. I'm not sure what the convention is for sending a file via byte data over to an iOS device. Any help would be appreciated.
Been there, but on the iOS end.
What we did to solve this problem was to have the .NET server send the image as a base64 encoded string, just as you said.
In the iOS end, the dev must then use a base64 decoding class (there are plenty on the internet, this one for example: http://www.imthi.com/wp-content/uploads/2010/08/base64.zip), transform that into NSData and then create the image with that data.
Should look something like this:
NSString *b64string = #"string to decode";
NSData *imgData = [DecoderClass decodeBase64FromString:b64String];
UIImage *image = [UIImage imageWithData:imgData];
If the data is correct, it should work.
UIImage is a high-level class and should handle image types (jpg, png, etc) automatically, as long as the data is consistent.

C# Download Data using Web Client

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.

Categories

Resources