Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

image converting

eligetieligeti Posts: 3Member


I created an System.Drawing.Image object from Bitmap from a bmp file. Now i want to convert this image object to JPEG format image object without saving into a JPEG file intermediately. How can i do this?

thanks,
vinod

Comments

  • iwilld0itiwilld0it Posts: 1,134Member
    [b][red]This message was edited by iwilld0it at 2004-12-30 7:34:45[/red][/b][hr]
    :
    :
    : I created an System.Drawing.Image object from Bitmap from a bmp file. Now i want to convert this image object to JPEG format image object without saving into a JPEG file intermediately. How can i do this?
    :
    : thanks,
    : vinod
    :

    [code]
    Using System.IO;
    Using System.Drawing;
    Using System.Drawing.Imaging;

    :

    public Image ConvertImage(Image oldImage, ImageFormat format)
    {
    // Do not convert if the source and target formats are the same
    if(oldImage.RawFormat.Equals(format))
    return oldImage;

    // To store new image
    Image newImage;

    // Creates an in-memory array of bytes
    MemoryStream buffer = new MemoryStream();

    // Stores source image in byte array, in the desired image format
    oldImage.Save(buffer, format);
    oldImage.Dispose();

    // Creates the new image object from the byte array
    newImage = Image.FromStream(buffer);
    buffer.Close();

    return newImage;
    }

    public Image ConvertImage(String imagePath, ImageFormat format)
    {
    return ConvertImage(Image.FromFile(imagePath), format);
    }
    [/code]

    These functions are overloaded. The first function takes an Image object and converts it to another Image object. The second function creates an image object based on file name and converts it to another Image type.

    Example (Place a Picture Control On Form)
    [code]
    Image img = ConvertImage("c:\test.bmp", ImageFormat.Jpeg);

    this.PictureBox1.Image = img;

    MessageBox.Show(img.RawFormat.Equals(ImageFormat.Jpeg).ToString());
    [/code]


Sign In or Register to comment.