Operate stream and byte array in C #

Source: Internet
Author: User
C # operating stream and byte Array2008-08-01

This article is Frank's learning notes. For more information, see the original article link.

In C #, the stream sent by post and byte Arrays' operations seem to contain very few data files. below is my research result over the past few days. The function is to copy a.jpg under the same file to generate B .jpg.CodeAs follows:

Bytearray. aspx. CS

Using system;

Using system. IO;

Using system. Data;

Using system. drawing;

Using system. Drawing. imaging;

Using system. net;

/*@

Author: Frank

Site: www.2solo.cn

Date: 2008.02.20

Info: C # copy the image, stream and bytearray application, and generate the image part

*/

Namespace barray {

Public partial class imghandler: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Try
{
Stream sin = page. Request. inputstream;
System. Drawing. Image IMG = system. Drawing. bitmap. fromstream (SIN );
Bitmap BMP = new Bitmap (IMG );
Memorystream BMP stream = new memorystream ();
BMP. Save (BMP stream, system. Drawing. imaging. imageformat. JPEG );
Filestream FS = new filestream (system. Web. httpcontext. Current. server. mappath ("B .jpg"), filemode. Create );
BMP stream. writeto (FS );
BMP stream. Close ();
FS. Close ();
BMP stream. Dispose ();
FS. Dispose ();
Response. Write ("successful ");
}
Catch
{
Response. Write ("failed ");
}
}
}
}

Gopost. aspx. CS

Using system;

Using system. IO;

Using system. drawing;
Using system. Drawing. imaging;

Using system. net;
Using system. text;
/*@
Author: Frank

Site: www.2solo.cn
Date: 2008.02.20

Info: C # copy the image, stream and bytearray application, and submit the image Section
*/
Namespace gopost

{
Public partial class posthandler: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{

Postimage ();
}

Private void postimage ()

{
Try
{
Httpwebrequest request;
String imgurl = system. Web. httpcontext. Current. server. mappath ("a.jpg ");
Request = (httpwebrequest) httpwebrequest. Create (http: // localhost/bytearray. aspx );
Request. keepalive = true;
Request. method = "Post ";
Byte [] bytearray = cvtimgbarr (system. Drawing. Image) New Bitmap (@ imgurl), imageformat. JPEG );
Request. contenttype = "image/JPEG ";
Request. contentlength = bytearray. length;
Stream newstream = request. getrequeststream ();
Newstream. Write (bytearray, 0, bytearray. Length );
Newstream. Close ();
Response. Write ("image copied ");
}
Catch
{

Response. Write ("failed to copy the image ");
}

}

Private Static byte [] cvtimgbarr (system. Drawing. Image imagetoconvert, imageformat formatofimage)

{

Byte [] imarr;
Try
{
Using (memorystream myms = new memorystream ())
{

Imagetoconvert. Save (myms, formatofimage );

Imarr = myms. toarray ();
}
}

Catch (exception) {Throw ;}
Return imarr;
}

}
}

Relatively speaking, the application of byte array in HTML forms may be ignored all the time, but correct application of byte array can be greatly optimized.ProgramAnd make some unexpected results. Of course, I studied byte array mainly for the recent flash research.

Attached sample file: http://www.2solo.cn/upload/cbyteArray_2soloDOTcn.zip

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.