Click here to Skip to main content
15,868,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got some issue like this: I convert loaded image into some fixed format like 180X35 using Image Thumbnail () method.
But coming to .png Image, I got black color image not shown the actual image looking.
How to reslove these issues?
I'm using the following code:

C#
static void foo()
{
    if (ImgImages.UploadedFiles.Count > 0)
    {

        UploadedFile file = ImgImages.UploadedFiles[0];
        fileData = new byte[file.InputStream.Length];
        System.Drawing.Image myImage = System.Drawing.Image.FromStream(file.InputStream);
        // Compute thumbnail size.
        Size thumbnailSize = GetThumbnailSize(myImage);
        // Get thumbnail.
        System.Drawing.Image thumbnail = myImage.GetThumbnailImage(
            thumbnailSize.Width,
            thumbnailSize.Height,
            null, IntPtr.Zero
        );
        fileData = imageToByteArray(thumbnail);

        oCheck.Signature = fileData;

    }
}

static Size GetThumbnailSize(System.Drawing.Image original)
{
    // Maximum size of any dimension.
    const int MaxWidth = 180;
    const int Maxheight = 35;
    // Width and height.
    int originalWidth = original.Width;
    int originalHeight = original.Height;

    // Compute best factor to scale entire image based on larger dimension.
    double factor;
    double factor1;
    if (originalWidth > originalHeight)
    {
        factor = (double)MaxWidth / originalWidth;
        factor1 = (double)Maxheight / originalHeight;
    }
    else
    {
        factor = (double)MaxWidth / originalWidth;
        factor1 = (double)Maxheight / originalHeight;
    }

    // Return thumbnail size.
    //return new Size((int)(originalWidth * factor), (int)(originalHeight * factor));
    return new Size((int)(originalWidth * factor), (int)(originalHeight * factor1));
}

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    return ms.ToArray();
}


How to convert .Png Image into .jpeg or .gif format,with out using image.save( "c:\imag.jpeg",.....)?

Here I don't want to "save" that image into My computer.
Once loaded .Png image into .Jpeg ...I want to store into byte[] or assign convert image to some image object.
I don't want save that converted image into "My computer " space.
Posted
Updated 13-Jun-13 19:35pm
v4
Comments
johannesnestler 3-Jun-13 10:12am    
did you solve it in between?

C#
if (ImgImages.UploadedFiles.Count > 0)
{
    UploadedFile file = ImgImages.UploadedFiles[0];
    fileData = new byte[file.InputStream.Length];
    System.Drawing.Image myImage = System.Drawing.Image.FromStream(
        file.InputStream
    );
    // Compute thumbnail size.
    Size thumbnailSize = GetThumbnailSize(myImage);
    // Get thumbnail.
    System.Drawing.Image thumbnail = myImage.GetThumbnailImage(
        thumbnailSize.Width,
        thumbnailSize.Height,
        null, IntPtr.Zero
    );
    fileData = imageToByteArray(thumbnail);
 
    oCheck.Signature = fileData;
}
 
static Size GetThumbnailSize(System.Drawing.Image original)
{
    // Maximum size of any dimension.
    const int MaxWidth = 180;
    const int Maxheight = 35;
    // Width and height.
    int originalWidth = original.Width;
    int originalHeight = original.Height;
 
    // Compute best factor to scale entire image based on larger dimension.
    double factor;
    double factor1;
    if (originalWidth > originalHeight)
    {
        factor = (double) MaxWidth / originalWidth;
        factor1 = (double)Maxheight / originalHeight;
    }
    else
    {
        factor = (double)MaxWidth / originalWidth;
        factor1 = (double)Maxheight / originalHeight;
    }

    // Return thumbnail size.
    //return new Size((int)(originalWidth * factor), (int)(originalHeight * factor));
    return new Size((int)(originalWidth * factor), (int)(originalHeight * factor1));
}
 
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    return ms.ToArray();
}
 
Share this answer
 
v2
Without saving to disk, there is nothing like .jpg or .png or whatever image file format. Therefore, you don't need to convert anything in this respect.

But you need to convert the data you get into something you can work with in memory. I strongly suggest to use the System.Drawing.Image[^] class for that. It's far superior to a byte array when having to deal with image manipulation. Transferring over a network or really fast image processing is another thing.

Since Image is an abstract class, you need to create instances of one of its derived classes. In your case, talking of .jpg and .png, use one of the System.Drawing.Bitmap[^] constructors.

Your next test is to assign the image to a System.Windows.Forms.PictureBox[^] to check if you can see it. When all images load correctly, you can step further and manipulate sizes/colours/whatever.
 
Share this answer
 
Hi sairam pamidi,

If I understand you right your Problem is just how to make an in-memory copy of your Image while changing the format? You could do something like this:

C#
static Image ConvertImage(Image imageOriginal, ImageFormat formatTarget)
{
    Image imageConverted = null;

    using (MemoryStream ms = new MemoryStream())
    {
        imageOriginal.Save(ms, formatTarget);

        ms.Position = 0; // rewind stream

        imageConverted = Image.FromStream(ms);

    }

    return imageConverted;
}


and call the function like this:

C#
Bitmap bmp = new Bitmap(@"C:\temp\test.png");
Bitmap bmpConverted = ConvertImage(bmp, ImageFormat.Gif) as Bitmap;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900