以二进制形式存取图片

以二进制形式存取图片

注:此图片表中类型为 text、ntext、image格式才可存入数据库

        /// <summary>
        /// 以二进制形式存取图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
      

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            try
            {
                this.pictureBox1.Image = Image.FromStream(this.openFileDialog1.OpenFile());
                string str = openFileDialog1.FileName.ToString();
                //将指定路径的文件添加到FileStream类中
                FileStream fs = new FileStream(str, FileMode.Open, FileAccess.Read);
                //通过FileStream类对象实例化BinaryReader类
                BinaryReader br = new BinaryReader(fs);
                //通过BinaryReader类对象的ReadBytes()方法将FileStream类对象转化为二进制数组
                imgBytesIn = br.ReadBytes((int)fs.Length);
                //MessageBox.Show(imgBytesIn.ToString());
            }
            catch
            {
                MessageBox.Show("您选择的图片不能被读取或文件类型不对!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.pictureBox1.Image = null;
            }
        }
        string tablename = "cpic";
        private void btAddPic_Click(object sender, EventArgs e)
        {
            //new Db().InsertInfo_Clpic(cph, imgBytesIn);
            if (this.pictureBox1.Image == null)
            {
                return;
            }
            new Db().InsertInfo_Clpic(tablename,cph, imgBytesIn);
            new Db().CloseConnection();
            btAddPic.Enabled = false;
            MessageBox.Show("添加成功!");
            btUpPic.Enabled = true;
            btDelPic.Enabled = true;
        }

package { import flash.display.Bitmap; import flash.display.Loader; import flash.display.NativeMenu; import flash.display.NativeMenuItem; import flash.display.NativeWindow; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.filesystem.File; import flash.filesystem.FileMode; import flash.filesystem.FileStream; import flash.net.FileFilter; import flash.net.navigateToURL; import flash.net.URLRequest; import flash.utils.ByteArray; public class Main extends Sprite { private var rootMenu:NativeMenu = new NativeMenu; private var fileMenu:NativeMenu = new NativeMenu; private var openItem:NativeMenuItem = new NativeMenuItem("打开文件"); private var quitItem:NativeMenuItem = new NativeMenuItem("退出"); private var helpMenu:NativeMenu = new NativeMenu; private var fsnhf:NativeMenuItem = new NativeMenuItem("00"); private var window:NativeWindow = stage.nativeWindow; private var loader:Loader = new Loader; private var imageFileFilter:FileFilter = new FileFilter("图片(*.jpg;*.png;*.gif;*.jpeg)", "*.jpg;*.png;*.gif;*.jpeg"); public function Main():void { stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; buildMenu(); configListeners(); } private function buildMenu():void { rootMenu.addSubmenu(fileMenu, "文件"); fileMenu.addItem(openItem); fileMenu.addItem(quitItem); rootMenu.addSubmenu(helpMenu, "帮助"); helpMenu.addItem(fsnhf); window.menu = rootMenu; } private function configListeners():void { openItem.addEventListener(Event.SELECT, eventHandler); quitItem.addEventListener(Event.SELECT, eventHandler); fsnhf.addEventListener(Event.SELECT, eventHandler); } private function eventHandler(event:Event):void { switch(event.target) { case openItem: var file:File = new File; file.addEventListener(Event.SELECT, selectFile); file.browseForOpen("打开文件", [imageFileFilter]); break; case quitItem: window.close(); break; case fsnhf: navigateToURL(new URLRequest("00")); break; } } private function selectFile(event:Event):void { var file:File = event.target as File; var stream:FileStream = new FileStream; stream.open(file, FileMode.READ); var bytes:ByteArray = new ByteArray; stream.readBytes(bytes, 0, stream.bytesAvailable); stream.close(); loader.unload(); loader = new Loader; loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded); loader.loadBytes(bytes); addChild(loader); } private function imageLoaded(event:Event):void { event.target.removeEventListener(Event.COMPLETE, imageLoaded); var bitmap:Bitmap = Bitmap(event.target.loader.content); window.width = bitmap.width; window.height = bitmap.height + 30; } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值