Use C # To make the image format Converter

Source: Internet
Author: User
Tags image converter

Visual C # is the next-generation programming language launched by Microsoft. It is Microsoft. an important part of the. Net Framework. In the process of launching Visual C #, Microsoft also launched a software development kit --.. Net FrameWork SDK. This software development kit encapsulates many classes and objects. Visual C # implements many powerful functions by calling these classes and objects.

-. Overview:

Because C # calls many powerful classes and objects, C # And. net Framework is very convenient to implement conversion between different image formats. However, if you use Windows APIs, the workload will be very large because there is no direct function support. In C #, you only need to use the ImageFormat class (in the System. Drawing. Imaging namespace) to convert the following common image formats.

BMP-extended name .bmp

GIF-extended name .gif

Icon-extension. ico

JPEG-extended name .jpg,. jpeg

PNG-extended name .png

TIFF-extension. tif

WMF-extension. wmf

II. Environment for program design and operation:

(1) Microsoft Windows 2000 Server Edition

(2). Net FrameWrok SDK Beta 2

3. Specific steps:

(1) create a C # project under Visual Studio and name it Image Converter. The figure is as follows:

(2) Now let's design the main interface of the program:

First, set the Text attribute of the form to Image Converter.

Add two Button controls, two ComboBox controls, and one PictureBox to the form:

Set the Name attributes of the two Button controls to m_btnOpen and m_btnSaveAs respectively, and set the Text attributes to "open" and "convert to" respectively (You may wish to set their FlatStyle attributes to Flat, because currently Flat is popular !).

Set the Name attributes of the two ComboBox controls to m_cmbOpen and m_cmbSaveAs respectively, and set their Text attributes *. bmp, and add * to the string Set editor in their Items attributes *. bmp *. jpg *. gif *. tif (one per line ).

Finally, we set the Name attribute of the PictureBox control to m_pictureBox, and changed its BorderStyle attribute to FixedSingle for the sake of appearance.

Okay, so far we have done all the interface work well. The figure is as follows:


3). Next we will write the code section (right-click the form and click "view code" to open the code file ):

First, add using System at the beginning of the code file. drawing. imaging; because this program uses the ImageFormat class, this class is in the System. drawing. this sentence must be added to the Imaging namespace.

Next, add three private data members to our class: private Bitmap m_bitmap; private int m_width0; private int m_height0; initialize the three data members in the constructor. The Code is as follows:

Public Form1 ()

{

//

// Required for Windows Form Designer support

//

InitializeComponent ();

//

// TODO: Add any constructor code after InitializeComponent call

//

M_bitmap = null;

M_width0 = m_pictureBox.Size.Width;

M_height0 = m_pictureBox.Size.Height;

}

Finally, add a Click event to the "open" and "convert to" buttons to generate two message-related functions. The code and comments are as follows:

Private void m_btnOpen_Click (object sender, System. EventArgs e)

{

File: // createCreate a dialog box object

OpenFileDialog ofd = new OpenFileDialog ();

File: // setSet Properties of the dialog box

Ofd. Filter = m_cmbOpen.Text + "|" + m_cmbOpen.Text;

String filter = ofd. Filter;

Ofd. InitialDirectory = System. Environment. CurrentDirectory;

Ofd. Title = "opening an image file ";

Ofd. ShowHelp = true;

If (ofd. ShowDialog () = DialogResult. OK)

{

File: // suchIf it is OK, an image object is created.

String strFileName = ofd. FileName;

M_bitmap = new Bitmap (strFileName );

File: // callResize m_pictureBox to fit the image size

If (m_bitmap.Width> m_bitmap.Height)

{

File: // saveHolding width

M_pictureBox.Width = m_width0;

M_pictureBox.Height = (int) (double) m_bitmap.Height * m_width0/m_bitmap.Width );

}

Else

{

File: // saveHeight holding

M_pictureBox.Height = m_height0;

M_pictureBox.Width = (int) (double) m_bitmap.Width * m_height0/m_bitmap.Height );

}

File: // displayImage Display

M_pictureBox.Image = m_bitmap;

File: // setSet the Form title

This. Text = "Image Converter:" + strFileName;

M_btnSaveAs.Enabled = true;

}

}

Private void m_btnSaveAs_Click (object sender, System. EventArgs e)

{

File: // createCreate an object for saving the dialog box

SaveFileDialog sfd = new SaveFileDialog ();

File: // setSet Properties of the dialog box

Sfd. Title = "converted ";

Sfd. OverwritePrompt = true;

Sfd. CheckPathExists = true;

Sfd. Filter = m_cmbSaveAs.Text + "|" + m_cmbSaveAs.Text;

Sfd. ShowHelp = true;

If (sfd. ShowDialog () = DialogResult. OK)

{

File: // suchIf it is OK, files in the corresponding format will be saved according to different options

String strFileName = sfd. FileName;

Switch (m_cmbSaveAs.Text)

{

Case "*. bmp ":

// Use the ImageFormat class here

M_bitmap.Save (strFileName, ImageFormat. Bmp );

Break;

Case "*. jpg ":

// Use the ImageFormat class here

M_bitmap.Save (strFileName, ImageFormat. Jpeg );

Break;

Case "*. gif ":

// Use the ImageFormat class here

M_bitmap.Save (strFileName, ImageFormat. Gif );

Break;

Case "*. tif ":

// Use the ImageFormat class here

M_bitmap.Save (strFileName, ImageFormat. Tiff );

Break;

}

This. Text = "Image Converter:" + strFileName;

}

}


4). OK. Press Ctrl + F5 to try the effect. The figure is as follows:

Iv. Summary:

Above, we quickly made an image format converter using C #, from which we can see the powerful functions of the class in C. In addition, C # has many classes and objects. If you can use them flexibly, you can develop many powerful applications.

For the above program, we can also extend its functions, such as creating a tool for batch conversion of image formats. In this case, we do not need to open the file one by one and then convert it. We can convert all the image files in a directory into the file format we need, I think this is very useful when processing the image format in web page creation. Interested readers can try to extend this function.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.