i am saving image into database as binary format, and while retrieving i am converting that binary format into image file and displaying.
byte[] picbin = (byte[])(binary data in datbase);
ImageConverter ic = new ImageConverter();
System.Drawing.Image img = (System.Drawing.Image)ic.ConvertFrom(picbin);`
and saving img- variable into some folder, by giving some name. but now i need to able to provide "downloadable option" to the user for that particular image in any one format, so that user can verify it so what is the way to implement this.
To convert image to different format:
// Save the image in JPEG format.
img.Save(#"C:\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
// Save the image in GIF format.
img.Save(#"C:\test.gif", System.Drawing.Imaging.ImageFormat.Gif);
// Save the image in PNG format.
img.Save(#"C:\test.png", System.Drawing.Imaging.ImageFormat.Png);
Check How to: Convert Images from One Format to Another
Related
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.
Yes you can just use:
// A byte array that contains a .jpeg data.
System.IO.Stream BitmapStream = System.IO.MemoryStream(byteBuffer);
System.Drawing.Bitmap MyImage = System.Drawing.Bitmap.FromStream(BitmapStream);
MyImage.Save("C:\Folder\Folder\image.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
But how could you do this without System.Drawing?
I would like to write my own code to create the image.
If you have the image in bytes you don't need to use drawing to make an image, just save it as a binary file.
System.IO.File.WriteAllBytes("C:\Folder\Folder\image.png", byteBuffer);
I have an file name data.yuv ( It's contain image data in YUV format).
What I need to do to convert the file to jpg or bmp image with C# ?
If I save the yuv file to image. It's show an green image. Thus, I need codes to convert data from yuv to image.
Open that file by using a FileStream and use the constructor of Bitmap to convert the stream to a valid bitmap.
FileStream fs = new FileStream("somefile.yuv", FileMode.Open);
Bitmap bmp = new Bitmap(fs);
I want to save a file from a request. How can I change the format of an image and how can I rename them?
Can anyone show me code to rename and convert the format of an image to PNG and save them on server?
How do you retrieve the file and what format is the file in?
You might be able to load it into a Bitmap and then save it using the Bitmap.Save(String, ImageFormat) method. There you can specify the name of the file as well.
This is actually very easy:
// Load the image.
System.Drawing.Image image1 = System.Drawing.Image.FromFile(#"C:\test.bmp");
// Save the image in JPEG format.
image1.Save(#"C:\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
// Save the image in GIF format.
image1.Save(#"C:\test.gif", System.Drawing.Imaging.ImageFormat.Gif);
// Save the image in PNG format.
image1.Save(#"C:\test.png", System.Drawing.Imaging.ImageFormat.Png);
I found that using BmpBitmapEncoder to render any type of image works, the only thing I'd need to do is send the correct format in the file to be saved as in the following example:
BmpBitmapEncoder encoder = new BmpBitmapEncoder();;
encoder.Frames.Add(BitmapFrame.Create(renderer));
using (System.IO.FileStream fs = System.IO.File.Open("file.png", System.IO.FileMode.OpenOrCreate))
{
encoder.Save(fs);
}
So, as you can see, the name of the image is "file.png", and this works correctly, it saves the image as PNG (also works with jpeg, tiff, gif), and it can be loaded with any image processing application.
I just want to know how is this different from using the correct encoder for each type (PngBitmapEncoder, JpegBitmapEncoder, GifBitmapEncoder, etc) instead.
Thank you.
You MUST use the right encoder PngBitmapEncoder, JpegBitmapEncoder, GifBitmapEncoder.
The file you are saving this way is ALWAYS a BMP!
What is happening in your test is that the image processing application you are using is ignoring the extension and recognizing the real file format as a BMP.