How to get image (mediaFile) from image storage path (File.path) - c#

I have multiple local storage image path which is stored in sqlite and I have to upload these image on one click.
I have a string image path but I am not sure about how to use this.
I have store image path userinfo.FileName_LHSPic to list and now I want to get image and upload to server.
FileNameUpload userinfo = new FileNameUpload();
var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
{
CompressionQuality = 50,
PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium
});
userinfo.FileName_LHSPic = file.Path;

One option is to use 'file' and turn it into a byte array, and store that in your database.
When it's time to upload, stream the byte array to the server (as part of a wider scoped class), and decode the file at the server end.

Related

How can I get image content as pixel and create image from it in ASP.NET Core 6 Web API?

I try to save my images on my server, but I can't let my server save file and virus because of that I want to get image content as pixels of rgb and after that I create image by myself.
I can't use bitmap (or other type in C# like bitmapImage, ... etc) and I don't know how I can do this with sixlabors.ImageSharp.
I have some code that I tried but I can't implement the exact logic that I want (code shown here):
[HttpPost("[action]")]
public async Task<IActionResult> Get([FromForm] ImageFormat file)
{
await using var memoryStream = new MemoryStream();
await file.File.CopyToAsync(memoryStream);
IImageFormat format;
using (var image = Image.Load(memoryStream.ToArray(), out format))
{
using (var output = new MemoryStream())
{
image.Save(output, format);
var responseType = format.Name.ToLower();
return File(output.ToArray(), "application/octet-stream", file.File.FileName);
}
}
return null;
}
Can anybody help me with this problem?
i don't see a reason to convert image into image: there are several format zip-algorythms etc.wich you have to support in that case. example jpg is not bitmap, there is convertion issue - quality of image becomes less each conversion time. Image itself is not executable - it can be used only as container for virus body, can't harm your OSystem itself, another executable part should works somewhere.
But even if you would like to store images on disk, in other format - you can convert image to base64 text (one line of code, like example) - it less harmful and well known way to work with any file type. you can zip image by cszip, you can change file name and extension to hide file type.
I don't see a reasson to convert one image to another for this scenario/task.

RestSharp upload image with imagesource. (Image exist only as Variable)

How to upload a Image with RestSharp without using a local path.
Image exists only in a Image Variable.
All i found was with a string as path, but this is not the way i want to go.
Or is it possible to get the string Path from a ImageSource?
How can this be solved?
My current code:
var client = new RestClient();
var request = new RestRequest(PostImageUrl, Method.Post);
request.AddOrUpdateHeader("Content-Type", "multipart/form-data");
request.AddFile("image", bauzeichnung);
request.AlwaysMultipartFormData = true;
var response = client.Execute(request);
It is not possible, every file is need a source. But I can suggest methods that I do not recommend.
You can convert image to base64 string and save image as string
You can take Url of image and show in your app. But in this case image is should be updated to network before(Url).
You can ConvertImage to base64 and store it in your cache.
You can convert Image to ByteArray.
None of these recommendations are correct methods!!!
The most accurate method;
You can take the image and store it in your machine(Local or Server). Then tahe the path of image and save database as Url(If you don't know the difference between url and path, you should research it). When you send image just send the url of your image.

C#.Net Download Image from URL, Crop, and Upload without Saving or Displaying

I have a large number of images on a Web server that need to be cropped. I would like to automate this process.
So my thought is to create a routine that, given the URL of the image, downloads the image, crops it, then uploads it back to the server (as a different file). I don't want to save the image locally, and I don't want to display the image to the screen.
I already have a project in C#.Net that I'd like to do this in, but I could do .Net Core if I have to.
I have looked around, but all the information I could find for downloading an image involves saving the file locally, and all the information I could find about cropping involves displaying the image to the screen.
Is there a way to do what I need?
It's perfectly possible to issue a GET request to a URL and have the response returned to you as a byte[] using HttpClient.GetByteArrayAsync. With that binary content, you can read it into an Image using Image.FromStream.
Once you have that Image object, you can use the answer from here to do your cropping.
//Note: You only want a single HttpClient in your application
//and re-use it where possible to avoid socket exhaustion issues
using (var httpClient = new HttpClient())
{
//Issue the GET request to a URL and read the response into a
//stream that can be used to load the image
var imageContent = await httpClient.GetByteArrayAsync("<your image url>");
using (var imageBuffer = new MemoryStream(imageContent))
{
var image = Image.FromStream(imageBuffer);
//Do something with image
}
}

Storing image in database in asp.net core. Should I store the image in bytes of just stooring the path of uploaded file is enough

I am storing the image file details in the database. Along with the path, I am also planning to store the byte array in case of accidental deletion of the uploaded folder. I would be using SQL express database as I want to use the app locally. As the size of SQL express is 10GB is it advisable to store a byte array of images in the database.
Does the size of the byte array is same as the size of the image? As I am expecting the images to be of around 5-10MB.
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await postPatientInformationEntity.PatientPhoto.CopyToAsync(fileStream);
fileInformationEntity.Path = filePath;
fileInformationEntity.Name = postPatientInformationEntity.PatientPhoto.FileName;
fileInformationEntity.Type = postPatientInformationEntity.PatientPhoto.ContentType;
}
using (var ms = new MemoryStream())
{
await postPatientInformationEntity.PatientPhoto.CopyToAsync(ms);
var fileBytes = ms.ToArray();
fileInformationEntity.FileData = fileBytes;
// act on the Base64 data
}
An alternative, and better method is to store the images outside of the database and store only a link to the image file. You only need a text field in your database table to store this information. The only problem to this approach is that you must synchronize the data in the link field with your file system. The possible solution is to store it in the file system, if the images need to be accessed from another server, tools like rsync can be used to move files from one server to other. this tools can be automated in order to sync the filesystems in constant intervals. The path of uploaded file is enough.

FileUpload.FileBytes not storing all the image data

I have the following code to upload the image:
FileUpload uniformItemImageFileUpload = uniformItemsGrid.FooterRow.FindControl("UniformImageInsert") as FileUpload;
byte[] itemBytes = uniformItemImageFileUpload.FileBytes;
And the following to bind to the database:
OracleParameter itemImageParameter = new OracleParameter("itemImage", OracleDbType.Blob, 4000);
itemImageParameter.Value = itemImageBytes;
oraCommand.Parameters.Add(itemImageParameter);
I have a problem, that when the image is uploaded through this mechanism, it does not store all the binary data of that image, but it only stores part of it if it is an image greater then 4kb. However, the problem is not the storage, since the 4000 size of the blob can fit larger files, in fact if I upload it directly through the SQL Navigator it can store it fully, with no problems, however, when trying to upload the same image through the code, it does not store all of it.
Has any one encountered this problem? how can it be fixed?
You need to specify the size of the Blob to the OracleParameter, i.e.
OracleParameter itemImageParameter = new OracleParameter("itemImage", OracleDbType.Blob, itemBytes.Length);
itemImageParameter.Value = itemImageBytes;
oraCommand.Parameters.Add(itemImageParameter);
Hopefully that will work :)

Categories

Resources