Control study-> image format conversion example: Convert *. jpg-> *. gif

Source: Internet
Author: User

(1). Functions

A sample program that demonstrates image format conversion.
Supported formats: conversion between bmp jpeg gif tiff png wmf and others

(2). Image example

(2). Code (the entire background code)

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. IO;
Using system. Drawing. imaging;
Namespace image format conversion
{
/// <Summary>
/// Summary of form1.
/// </Summary>
Public class formmain: system. Windows. Forms. Form
{
Private system. Windows. Forms. Panel Panel1;
Private system. Windows. Forms. splitter splitter1;
Private system. Windows. Forms. Panel panel2;
Private system. Windows. Forms. Button buttonopen;
Private system. Windows. Forms. Button buttonconvert;
Private system. Windows. Forms. ComboBox;
Private system. Windows. Forms. Label label1;
Private system. Windows. Forms. Label label2;
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. Container components = NULL;
Private system. Windows. Forms. picturebox;
Private system. Drawing. Bitmap bitmap;

Public formmain ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();

//
// Todo: add Any constructor code after initializecomponent calls
//
Bitmap = NULL;
Picturebox. width = panel2.width;
Picturebox. Height = panel2.height;
Picturebox. Top = panel1.top;
Picturebox. Left = panel1.left;
Panel2.resize + = new system. eventhandler (panel2_resize );
// Buttonopen. Click + = new system. eventhandler (buttonopen_click );
ComboBox. Items. addrange (new object [] {
"*. Jpg ",
"*. BMP ",
"*. GIF ",
"*. PNG ",
"*. Tif ",
"*. WMF"
});

}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region windows Form Designer generated code
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Panel1 = new system. Windows. Forms. Panel ();
This. buttonconvert = new system. Windows. Forms. Button ();
This. buttonopen = new system. Windows. Forms. Button ();
This. ComboBox = new system. Windows. Forms. ComboBox ();
This. label1 = new system. Windows. Forms. Label ();
This. label2 = new system. Windows. Forms. Label ();
This. splitter1 = new system. Windows. Forms. Splitter ();
This. panel2 = new system. Windows. Forms. Panel ();
This. picturebox = new system. Windows. Forms. picturebox ();
This. panel1.suspendlayout ();
This. panel2.suspendlayout ();
This. suspendlayout ();
//
// Panel1
//
This. panel1.borderstyle = system. Windows. Forms. borderstyle. fixed3d;
This. panel1.controls. addrange (new system. Windows. Forms. Control [] {
This. buttonconvert,
This. buttonopen,
This. ComboBox,
This. label1,
This. label2 });
This. panel1.dock = system. Windows. Forms. dockstyle. Left;
This. panel1.name = "Panel1 ";
This. panel1.size = new system. Drawing. Size (128,365 );
This. panel1.tabindex = 0;
//
// Buttonconvert
//
This. buttonconvert. Location = new system. Drawing. Point (40,264 );
This. buttonconvert. Name = "buttonconvert ";
This. buttonconvert. size = new system. Drawing. Size (56, 24 );
This. buttonconvert. tabindex = 1;
This. buttonconvert. Text = "Conversion ";
Buttonconvert. Click + = new system. eventhandler (buttonconvert_click );
//
// Buttonopen
//
This. buttonopen. Location = new system. Drawing. Point (32, 56 );
This. buttonopen. Name = "buttonopen ";
This. buttonopen. size = new system. Drawing. Size (64, 24 );
This. buttonopen. tabindex = 0;
This. buttonopen. Text = "Open File ";
This. buttonopen. Click + = new system. eventhandler (this. buttonopen_click );
//
// ComboBox
//
This. ComboBox. Location = new system. Drawing. Point (64,224 );
This. ComboBox. Name = "ComboBox ";
This. ComboBox. size = new system. Drawing. Size (48, 20 );
This. ComboBox. tabindex = 0;
This. ComboBox. Text = "combobox1 ";
//
// Label1
//
This. label1.location = new system. Drawing. Point (24,104 );
This. label1.name = "label1 ";
This. label1.size = new system. Drawing. Size (80, 40 );
This. label1.tabindex = 0;
This. label1.text = "label1 ";
//
// Label2
//
This. label2.location = new system. Drawing. Point (8,224 );
This. label2.name = "label2 ";
This. label2.size = new system. Drawing. Size (48, 16 );
This. label2.tabindex = 0;
This. label2.text = "convert ";
//
// Splitter1
//
This. splitter1.location = new system. Drawing. Point (128, 0 );
This. splitter1.name = "splitter1 ";
This. splitter1.size = new system. Drawing. Size (3,365 );
This. splitter1.tabindex = 1;
This. splitter1.tabstop = false;
//
// Panel2
//
This. panel2.borderstyle = system. Windows. Forms. borderstyle. fixed3d;
This. panel2.controls. addrange (new system. Windows. Forms. Control [] {
This. picturebox });
This. panel2.dock = system. Windows. Forms. dockstyle. Fill;
This. panel2.location = new system. Drawing. Point (131, 0 );
This. panel2.name = "panel2 ";
This. panel2.size = new system. Drawing. Size (373,365 );
This. panel2.tabindex = 2;
//
// Picturebox
//
This. picturebox. Dock = system. Windows. Forms. dockstyle. Fill;
This. picturebox. Name = "picturebox ";
This. picturebox. size = new system. Drawing. Size (369,361 );
This. picturebox. sizemode = system. Windows. Forms. pictureboxsizemode. stretchimage;
This. picturebox. tabindex = 0;
This. picturebox. tabstop = false;
//
// Formmain
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (504,365 );
This. Controls. addrange (new system. Windows. Forms. Control [] {
This. panel2,
This. splitter1,
This. Panel1 });
This. Name = "formmain ";
This. startposition = system. Windows. Forms. formstartposition. centerscreen;
This. Text = "formmain ";
This. panel1.resumelayout (false );
This. panel2.resumelayout (false );
This. resumelayout (false );

}
# Endregion

/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main ()
{
Application. Run (New formmain ());
}

Private void buttonopen_click (Object sender, system. eventargs E)
{
Openfiledialog = new openfiledialog ();
Openfiledialog. filter = "*. JPG ,*. JPEG ,*. BMP ,*. GIF ,*. ICO ,*. PNG ,*. TIF ,*. WMF | *. JPG ;*. JPEG ;*. BMP ;*. GIF ;*. ICO ;*. PNG ;*. TIF ;*. WMF ";
Openfiledialog. Title = "open image file ";
Openfiledialog. multiselect = false;
If (openfiledialog. showdialog () = dialogresult. OK)
{
If (Bitmap! = NULL)
{
Bitmap. Dispose ();
}
String filename = openfiledialog. filename;
Bitmap = new Bitmap (filename );
If (bitmap. width> bitmap. width)
{
Picturebox. width = panel2.width;
Picturebox. Height = (INT) (double) bitmap. Height * panel2.width/bitmap. width );
}
Else
{
Picturebox. Height = panel2.height;
Picturebox. width = (INT) (double) bitmap. Width * panel2.height/bitmap. Height );
}
Picturebox. Image = bitmap;
Fileinfo F = new fileinfo (filename );
This. Text = "image conversion:" + F. Name;
This. label1.text = f. Name;
Buttonconvert. Enabled = true;
}
}
Private void panel2_resize (Object sender, system. eventargs E)
{
Picturebox. Top = panel1.top;
Picturebox. Left = panel1.left;
If (Bitmap! = NULL)
{
If (bitmap. width> bitmap. Height)
{
Picturebox. width = panel2.width;
Picturebox. Height = (INT) (double) bitmap. Height * panel2.width/bitmap. width );
}
Else
{
Picturebox. Height = panel2.height;
Picturebox. width = (INT) (double) bitmap. Width * panel2.height/bitmap. Height );
}
}
Else
{
Picturebox. width = panel2.width;
Picturebox. Height = panel2.height;
}
Picturebox. Refresh ();
}
Private void buttonconvert_click (Object sender, system. eventargs E)
{
If (ComboBox. selecteditem = NULL)
{
Return;
}
Else
{
Savefiledialog = new savefiledialog ();
Savefiledialog. Title = "converted :";
Savefiledialog. overwriteprompt = true;
Savefiledialog. checkpathexists = true;
Savefiledialog. Filter = ComboBox. Text + "|" + ComboBox. text;
If (savefiledialog. showdialog () = dialogresult. OK)
{
String filename = savefiledialog. filename;
Switch (ComboBox. Text)
{
Case "*. BMP ":
Bitmap. Save (filename, imageformat. BMP );
Break;
Case "*. jpg ":
Bitmap. Save (filename, imageformat. JPEG );
Break;
Case "*. GIF ":
Bitmap. Save (filename, imageformat. GIF );
Break;
Case "*. tif ":
Bitmap. Save (filename, imageformat. Tiff );
Break;
Case "*. PNG ":
Bitmap. Save (filename, imageformat. PNG );
Break;
Case "*. WMF ":
Bitmap. Save (filename, imageformat. WMF );
Break;

}
Fileinfo F = new fileinfo (filename );
This. Text = "image conversion:" + F. Name;
Label1.text = f. Name;
}
}
}
}
}

(4) Sample Code download

Http://www.cnblogs.com/files/chengking/graphic format conversion .rar

 

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.