How can I convert a file and save it on server? - c#

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

Related

Saving a image that was converted into byte[] without System.Drawing?

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

how to make image as downloadable from database in asp.net,C#

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

Convert yuv file to image in C#

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

How to save image stream in c#?

I have a imagestream in c# and i want to save them on the hard drive using the c# code. when i trying to do that i found Out of Memory whennever i have much enough Memory.
so i am sure that my code leak the resources so can someone show me how i can do that
HttpPostedFileBase file
file.SaveAs(location);
Image image = Image.FromFile(location,false);
image.Save(location, System.Drawing.Imaging.ImageFormat.Png);
image.fromFile line [3] caused Exception that out of Memory. can someone show me how i can do this in c#.
The file come from PNG using Ajax Request are come as octet type as Mime type so how i can do that.
Assuming you want to convert the image to PNG (otherwise there's no need to save the image once, reload it and re-save it again), it might be a good idea to avoid loading the image twice. See if something like this helps:
Image.FromStream(file.InputStream, false).Save(location, System.Drawing.Imaging.ImageFormat.Png);
file.SaveAs(location);
already saved the image at the given location or threw an exception, so the following lines are unnecessary.
Bitmap bmp = new Bitmap(file.InputStream);
bmp.Save(filename, ImageFormat.Png);

Using BmpBitmapEncoder for all image formats

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.

Categories

Resources