您的位置:程序门 -> .net技术 -> c#



如何使用memorystream保存image图片呢?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何使用memorystream保存image图片呢?
发表于:2007-03-22 22:26:47 楼主
请教大家一个问题,假如我已知了一个bytearrary,如何利用memorystream把这个bytearrary保存为一个xxx.png图片呢?
发表于:2007-03-22 22:34:161楼 得分:0
http://www.guodong.net/blog/article.asp?id=238
发表于:2007-03-22 22:36:132楼 得分:0
up
发表于:2007-03-22 22:38:393楼 得分:0
vs的帮助系统够强的了,一般不懂但又不难的东西我都自己找~~~
发表于:2007-03-22 23:51:414楼 得分:0
//--下面是一个生成   验证码的程序代码   楼主看看应该就能明白   输出类型改成.png即可

using   system;
using   system.collections;
using   system.componentmodel;
using   system.data;
using   system.drawing;
using   system.drawing.drawing2d;
using   system.drawing.imaging;
using   system.io;
using   system.web;
using   system.web.sessionstate;
using   system.web.ui;
using   system.web.ui.webcontrols;
using   system.web.ui.htmlcontrols;


public   partial   class   validate   :   system.web.ui.page
{
        private   const   int   rndlength   =   4;
        protected   void   page_load(object   sender,   eventargs   e)
        {
                //   在此处放置用户代码以初始化页面
                string   vnum;
                vnum   =   getbyrndnum();
                session[ "validatecode "]   =   vnum;

                response.clearcontent();   //需要输出图象信息   要修改http头  
                //response.bufferoutput   =   false;
                response.contenttype   =   "image/jpeg ";
                response.binarywrite(getbyvalidatecode(vnum).toarray());
                //response.close();

        }

        private   memorystream   getbyvalidatecode(string   vnum)//返回内存流
        {
                bitmap   img   =   null;
                graphics   g   =   null;
                memorystream   ms   =   null;
                random   random   =   new   random();
                int   gheight   =   vnum.length   *   12;
                img   =   new   bitmap(gheight,   20);
                g   =   graphics.fromimage(img);
                font   f   =   new   font( "arial ",   12,   fontstyle.bold);

                g.clear(getbyrandcolor(180,   200));//设定背景色
                pen   blackpen   =   new   pen(color.antiquewhite,   1);
                //pen   blackpen   =   new   pen(color.black   ,1);
                for   (int   i   =   0;   i   <   32;   i++)//   随机产生干扰线,使图象中的认证码不易被其它程序探测到
                {
                        int   x   =   random.next(gheight);
                        int   y   =   random.next(20);
                        int   xl   =   random.next(6);
                        int   yl   =   random.next(12);
                        g.drawline(blackpen,   x,   y,   x   +   xl,   y   +   yl);
                }
                solidbrush   s   =   new   solidbrush(color.saddlebrown);
                g.drawstring(vnum,   f,   s,   3,   3);
                ms   =   new   memorystream();
                img.save(ms,   system.drawing.imaging.imageformat.jpeg);//jpeg改png即可
                g.dispose();
                img.dispose();
                return   ms;
        }
        //-----------------给定范围获得随机颜色------------
        color   getbyrandcolor(int   fc,   int   bc)
        {
                random   random   =   new   random();

                if   (fc   >   255)   fc   =   255;
                if   (bc   >   255)   bc   =   255;
                //if(ac> 255)   ac=255;
                int   r   =   fc   +   random.next(bc   -   fc);
                int   g   =   fc   +   random.next(bc   -   fc);
                int   b   =   fc   +   random.next(bc   -   bc);
                color   rs   =   color.fromargb(r,   g,   b);
                return   rs;
        }

        //-----------------------取随机产生的认证码(n位数字)
        public   string   getbyrndnum()
        {
                string   vchar   =   "0,1,2,3,4,5,6,7,8,9 ";//,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,w,x,y,z "   ;
                string[]   vcarray   =   vchar.split( ', ');
                string   vnum   =   " ";                
                //采用一个简单的算法以保证生成随机数的不同
                random   rand   =   new   random();
                int[]   rndarr   =   new   int[rndlength];
                for   (int   i   =   1;   i   <   rndlength   +   1;   i++)
                {      
                        if(i> 1)
                        {
                                do
                                {
                                        rndarr[i-1]   =   rand.next(10);//rand.next(35)   ;
                                        bool   goon   =   false;
                                        for(int   ix=0;   ix <i-1;   ix++)
                                        {
                                                if   (rndarr[ix]   ==   rndarr[i-1])
                                                {
                                                        goon   =   true;
                                                        break;
                                                }
                                        }
                                        if   (!goon)
                                                break;
                                }   while   (1   ==   1);
                        }
                        else
                                rndarr[i-1]   =   rand.next(10);
                        vnum   +=   rndarr[i-1];
                }
                return   vnum;
        }
}
发表于:2007-03-23 08:18:495楼 得分:0


                public   static   bool   bytes2file(byte[]   b,   string   file)
                {
                        bool   rtn   =   false;

                        filestream   fs   =   null;

                        try
                        {
                                fs   =   new   filestream(file,   filemode.create,   fileaccess.write);

                                fs.write(b,   0,   (int)b.length);

                                rtn   =   true;
                        }
                        catch
                        {
                                rtn   =   false;
                        }
                        finally
                        {
                                fs.close();
                        }

                        return   rtn;
                }
发表于:2007-03-23 08:54:086楼 得分:0
public   partial   class   validate   :   system.web.ui.page
{
        private   const   int   rndlength   =   4;
        protected   void   page_load(object   sender,   eventargs   e)
        {
                //   在此处放置用户代码以初始化页面
                string   vnum;
                vnum   =   getbyrndnum();
                session[ "validatecode "]   =   vnum;

                response.clearcontent();   //需要输出图象信息   要修改http头  
                //response.bufferoutput   =   false;
                response.contenttype   =   "image/jpeg ";
                response.binarywrite(getbyvalidatecode(vnum).toarray());
                //response.close();

        }

        private   memorystream   getbyvalidatecode(string   vnum)//返回内存流
        {
                bitmap   img   =   null;
                graphics   g   =   null;
                memorystream   ms   =   null;
                random   random   =   new   random();
                int   gheight   =   vnum.length   *   12;
                img   =   new   bitmap(gheight,   20);
                g   =   graphics.fromimage(img);
                font   f   =   new   font( "arial ",   12,   fontstyle.bold);

                g.clear(getbyrandcolor(180,   200));//设定背景色
                pen   blackpen   =   new   pen(color.antiquewhite,   1);
                //pen   blackpen   =   new   pen(color.black   ,1);
                for   (int   i   =   0;   i   <   32;   i++)//   随机产生干扰线,使图象中的认证码不易被其它程序探测到
                {
                        int   x   =   random.next(gheight);
                        int   y   =   random.next(20);
                        int   xl   =   random.next(6);
                        int   yl   =   random.next(12);
                        g.drawline(blackpen,   x,   y,   x   +   xl,   y   +   yl);
                }
                solidbrush   s   =   new   solidbrush(color.saddlebrown);
                g.drawstring(vnum,   f,   s,   3,   3);
                ms   =   new   memorystream();
                img.save(ms,   system.drawing.imaging.imageformat.jpeg);//jpeg改png即可
                g.dispose();
                img.dispose();
                return   ms;
        }
楼上的兄弟比较强
学学
发表于:2007-03-23 19:42:537楼 得分:0
private   void   button2_click_1(object   sender,   system.eventargs   e)
                {
                        int   icanvaswidth   =   2048;
                        int   icanvasheight   =   2048;
                        bitmap   bitmap   =   new   bitmap(icanvaswidth,   icanvasheight,   pixelformat.format24bpprgb);
                        graphics   graph   =   graphics.fromimage(bitmap);
                        graph.clear(color.white);
                        int   ix   =   40;
                        int   iy   =   40;
                        font   font   =   new   font( "ms   ui   gothic ",   12);
                        solidbrush   brush   =   new   solidbrush(color.black);
                        solidbrush   brush2   =   new   solidbrush(color.blue);
                        pen   pen   =   new   pen(color.black);                  
                        pen.endcap   =   linecap.arrowanchor;
                        pen.dashstyle   =   dashstyle.solid;
                        #region
                        graph.drawline(pen,   200,   200,   200,   2000);
                        graph.drawline(pen,   200,   1000,   1000,   1000);
                        for   (int   f   =   0;   f   <   20;   f++)
                        {
                                graph.drawline(pens.black,   200   +   ix   *   (f   +   1),   1000,   200   +   ix   *   (f   +   1),   1000   -   5);
                               
                        }
                        //y軸    
                        for   (int   f   =   0;   f   <   40;   f++)
                        {
                                graph.drawline(pens.black,   200,   200   +   iy   *   (f   +   1),   200   +   5,   200   +   iy   *   (f   +   1));
                                graph.drawline(pens.black,   200,   200   +   iy   *   (f   +   1),   200   +   5,   200   +   iy   *   (f   +   1));
                        }          
                        #endregion
                        #region
                        double   dbly1   =   1000;
                        double   dblx1   =   200;
                        for   (int   i   =   0;   i   <   arrdatay.count;   i++)
                        {
                                double   dbldata   =   convert.todouble(arrdatay[i]);
                                double   dbly2   =   1000   +   math.abs((dbldata   +   1.5)   /   (5   -   1.5)   *   800);
                                double   dblx2   =   (convert.todouble(arrxx[i])   -   1.5)   /   (5   -   1.5)   *   800   +   200;
                                dblx1   =   (convert.todouble(arrxx[i])   -   1.5)   /   (5   -   1.5)   *   800   +   203;
                                dbly1   =   1003   -   (dbldata   -   1.5)   /   (4   -   1.5)   *   800;
                                pen   blackpen   =   new   pen(color.blue,   1);
                                graph.drawellipse(blackpen,   float.parse(dblx2.tostring()),   float.parse(dbly2.tostring()),   4f,   4f);
                        }

                        for   (int   i   =   0;   i   <   arrdata.count;   i++)
                        {
                                double   dbldata   =   convert.todouble(arrdata[i]);
                                double   dbly2   =   1000   +   math.abs((dbldata   +   1.5)   /   (5   -   1.5)   *   800);
                                double   dblx2   =   (convert.todouble(arrx[i])   -   1.5)   /   (5   -   1.5)   *   800   +   200;
                                dblx1   =   (convert.todouble(arrx[i])   -   1.5)   /   (5   -   1.5)   *   800   +   203;
                                dbly1   =   1003   -   (dbldata   -   1.5)   /   (4   -   1.5)   *   800;
                                pen   blackpen   =   new   pen(color.blue,   1);
                                graph.drawellipse(blackpen,   float.parse(dblx2.tostring()),   float.parse(dbly2.tostring()),   4f,   4f);                  
                        }
                        for   (int   i   =   0;   i   <   min_id   -   20;   i++)
                        {
                                arrxx2.add((minx   -   xx19)   /   (min_id   -   20)   *   i   +   xx19);
                                arrdatay2.add(((minx   -   xx19)   /   (min_id   -   20)   *   i   +   xx19)   *   m1   /   m0   +   m2   /   m0);
                        }

                        pen   black3   =   new   pen(color.black,   1);
                        for   (int   i   =   0;   i   <   arrdatay2.count;   i++)
                        {
                                double   dbldata   =   convert.todouble(arrdatay2[i]);
                                double   dbly2   =   10   +   math.abs((dbldata   +   1.5)   /   (5   -   1.5)   *   800);
                                double   dblx2   =   (convert.todouble(arrxx2[i])   -   1.5)   /   (5   -   1.5)   *   800   +   200;
                                graph.drawellipse(black3,   float.parse(dblx2.tostring()),   float.parse(dbly2.tostring()),   4f,   4f);
                        }

                        for   (int   i   =   0;   i   <   20;   i++)
                        {
                                int   intx   =   800   /   20   *   i   +   200;
                                //int   intx   =   2/200*i   +   200;
                                int   inty   =   1000   -   800   /   20   *   i;
                                int   inty2   =   1000   +   800   /   20   *   i;
                                point   ptx   =   new   point(intx,   1000   -   25);
                                point   pty   =   new   point(200,   inty);
                                point   pty2   =   new   point(200,   inty2);
                                graph.drawstring(convert.tostring(i   *   (5   -   1.5)   /   20   +   1.5),   font,   brush2,   pty);
                                graph.drawstring( "- "   +   convert.tostring((i)   *   (5   -   1.5)   /   20   +   1.5),   font,   brush2,   pty2);
                                graph.drawstring(((5   -   1.5)   /   20   *   i   +   1.5).tostring(),   font,   brush2,   ptx);
                        }
                        #endregion

                        string   strfilepath   =   @ "d:\study\jackey_gif2.gif ";
                        bitmap.save(strfilepath,   imageformat.gif);
                        arrx.clear();
                        arrdata.clear();
                        arrxx.clear();
                        arrdatay.clear();
                        arrxx2.clear();
                        arrdatay2.clear();
                        font.dispose();
                        brush.dispose();
                        graph.dispose();
                        bitmap.dispose();
                }
发表于:2007-03-24 14:02:328楼 得分:0
imageconverter   imc   =   new   imageconverter();
image   _img   =   imc.convertfrom(null,   system.globalization.cultureinfo.currentculture,   bytearrary)   as   image;
if   (_img   !=   null)
{
//处理_img;
return;
}
发表于:2007-03-24 14:08:419楼 得分:0
或者
memorystream   ms   =   new   memorystream(bytearrary);
image   img   =   image.fromstream(ms);
发表于:2007-03-24 14:19:3310楼 得分:0
memorystream   ms   =   new   memorystream(bytearrary);
image   img   =   image.fromstream(ms);
----------就这个方法
发表于:2007-03-24 14:21:1611楼 得分:0
不是保存为图片吗?
用filestream
发表于:2007-03-24 14:22:1412楼 得分:0
filestream   fs   =   new   filestream(@ "c:\xxx.png ");
fs.write(bytearrary);
fs.close();
发表于:2007-03-24 14:24:1913楼 得分:0
filestream   fs   =   new   filestream(@ "c:\xxx.png ");
fs.write(bytearrary,0,fs.length);
fs.close();


快速检索

最新资讯
热门点击