C # basics of learning (dynamically open, display, save, and save as an image) [To be continued]

Source: Internet
Author: User

Statement: the basics come from this: http://blog.163.com/chenjunfeng_2007/blog/static/119385083201019102742570/

1. open:

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
Using system. IO;

Namespace windowsformsapplication5
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}

/* Use toolstripmenuitem to open a local image */

Private void open toolstripmenuitem_click (Object sender, eventargs E)
{
String picpath;
// Filter statement, "Open the lower part of the dialog box and ask for the displayed content | the real filter suffix name"
Openfiledialog1.filter = "image file (JPG, JPEG, BMP, GIF, ICO, pen, Tif) | *. JPG ;*. JPEG ;*. BMP ;*. GIF ;*. ICO ;*. PNG ;*. TIF ;*. WMF ";
Openfiledialog1.showdialog ();
Picpath = openfiledialog1.filename;
If (file. exists (picpath ))
{
Picturebox1.image = image. fromfile (picpath );
}
}

/* Use toolstripmenuitem to open a local image */

Private void save toolstripmenuitem_click (Object sender, eventargs E)
{
// Stream filestream;
Savefiledialog saveimagedialog = new savefiledialog ();
Saveimagedialog. Title = "save file instance ";
Saveimagedialog. Filter = @ "Jpeg | *. jpg | BMP | *. BMP | GIF | *. GIF ";

Saveimagedialog. showdialog ();

If (saveimagedialog. filename! = NULL)
{
// System. Io. Stream FS = (system. Io. Stream) saveimagedialog. openfile ();
// Filestream = new filestream (openfiledialog1.filename. tostring (), filemode. Open, fileaccess. Read );
String filename = saveimagedialog. filename. tostring ();

If (filename! = "" & Filename! = NULL)
{

/* Each character in the string is considered to have a digit position: the first character is 1, the second character is 2, and so on.

If the third parameter is not specified, the Child string from the position specified by the second parameter to the end of the string is returned.

If the parameter is not of the string type, the string () function is used to convert it to a string and then the conversion result is calculated.
Substring ("12345", 234) ---->)
Substring ("12345", 2) ------> (2345 )*/

/* Here fileextname is used as the switch statement's toggle, but there is a compilation Error */

String fileextname = filename. substring (filename. lastindexof ("."), 1). tostring ();
System. Drawing. imaging. imageformat = NULL;

If (fileextname! = NULL)
{
Switch (saveimagedialog. filterindex)
{
Case 1:
Imageformat = system. Drawing. imaging. imageformat. JPEG;
Break;
Case 2:
Imageformat = system. Drawing. imaging. imageformat. BMP;
Break;
}
If (imageformat = NULL)
{
Imageformat = system. Drawing. imaging. imageformat. JPEG;
}

Try
{
This. picturebox1.image. Save (filename, imageformat );
}
Catch
{
MessageBox. Show ("sorry, an exception occurred when saving the image ");
}
}
}
}
}

}
}

 

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.