Bmp to Jpg/Png in C#

Bmp to jpg/png in C#

var qualityEncoder = Encoder.Quality;
var quality = (long)<desired quality>;
var ratio = new EncoderParameter(qualityEncoder, quality );
var codecParams = new EncoderParameters(1);
codecParams.Param[0] = ratio;
var jpegCodecInfo = <one of the codec infos from ImageCodecInfo.GetImageEncoders() with mime type = "image/jpeg">;
bmp.Save(fileName, jpegCodecInfo, codecParams); // Save to JPG

Convert any image format to JPG

Get the memorystream and then use System.Drawing

var stream = new MemoryStream(byteArray)
Image img = new Bitmap(stream);
img.Save(@"c:\s\pic.png", System.Drawing.Imaging.ImageFormat.Png);

The last line there where you save the file, you can select the format.

Can bitmap object be save as PNG or JPEG file format

Bitmap extends Image, therefore you can call: Image.Save (String, ImageFormat). For example:

using System.Drawing
// ...

Bitmap img = new Bitmap("file.jpg");
img.Save("file.png", ImageFormat.Png); // ImageFormat.Jpeg, etc

Omitting the second argument and just calling Image.Save(String) will save the image as its raw format.

converting an image to png on upload

You're not converting it at all there.. you can use something like this:

using System.Drawing;

Bitmap b = (Bitmap)Bitmap.FromStream(file.InputStream);

using (MemoryStream ms = new MemoryStream()) {
b.Save(ms, ImageFormat.Png);

// use the memory stream to base64 encode..
}

Compressing Pixel data to jpg/png format

Try this:

        //example 1: converting from bitmap
Bitmap myImage1 = new Bitmap(@"C:\myimage1.bmp");

myImage1.Save(@"C:\myimage1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
myImage1.Save(@"C:\myimage1.png", System.Drawing.Imaging.ImageFormat.Png);

//example 2: converting from pixels
Bitmap myImage2 = new Bitmap(10, 10);

//for loop to set some pixels
for (int x=0;x<10;x++)
for (int y=0;y<10;y++)
myImage2.SetPixel(x,y,Color.Blue);

myImage2.Save(@"C:\myimage2.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
myImage2.Save(@"C:\myimage2.png", System.Drawing.Imaging.ImageFormat.Png);

Convert All to JPG in C#

I found the problem! It was caused by PNG alpha channel..

Thanks to max, link below solved my problem:

How to load Transparent PNG to Bitmap and ignore alpha channel


public static void SetAlpha(this Bitmap bmp, byte alpha)
{
if(bmp == null) throw new ArgumentNullException("bmp");

var data = bmp.LockBits(
new Rectangle(0, 0, bmp.Width, bmp.Height),
System.Drawing.Imaging.ImageLockMode.ReadWrite,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);

var line = data.Scan0;
var eof = line + data.Height * data.Stride;
while(line != eof)
{
var pixelAlpha = line + 3;
var eol = pixelAlpha + data.Width * 4;
while(pixelAlpha != eol)
{
System.Runtime.InteropServices.Marshal.WriteByte(
pixelAlpha, alpha);
pixelAlpha += 4;
}
line += data.Stride;
}
bmp.UnlockBits(data);
}

Usage:

var pngImage = new Bitmap("filename.png");
pngImage.SetAlpha(255);

after setting alpha it started to work correctly.

c# convert image formats to jpg

From: http://msdn.microsoft.com/en-us/library/system.drawing.imaging.encoderparameter.aspx

private void VaryQualityLevel()
{
// Get a bitmap.
Bitmap bmp1 = new Bitmap(@"c:\TestPhoto.jpg");
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);

// Create an Encoder object based on the GUID
// for the Quality parameter category.
System.Drawing.Imaging.Encoder myEncoder =
System.Drawing.Imaging.Encoder.Quality;

// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one
// EncoderParameter object in the array.
EncoderParameters myEncoderParameters = new EncoderParameters(1);

EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder,
50L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"c:\TestPhotoQualityFifty.jpg", jpgEncoder,
myEncoderParameters);

myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"c:\TestPhotoQualityHundred.jpg", jpgEncoder,
myEncoderParameters);

// Save the bitmap as a JPG file with zero quality level compression.
myEncoderParameter = new EncoderParameter(myEncoder, 0L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(@"c:\TestPhotoQualityZero.jpg", jpgEncoder,
myEncoderParameters);

}

...

private ImageCodecInfo GetEncoder(ImageFormat format)
{

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}

Upload image from Bitmap to server as png

You could save the Bitmap to a stream and upload the stream:

using (WebClient client = new WebClient())
using (var ms = new MemoryStream())
{
bitmap.Save(ms, ImageFormat.Png);
client.Credentials = new NetworkCredential("FTP_username", "FTP_password");
client.UploadData("ftp://100.100.100.100/new_folder/img_1.png", ms.ToArray());
}


Related Topics



Leave a reply



Submit