码迷,mamicode.com
首页 > Web开发 > 详细

利用FluorineFx的ByteArray上传图片

时间:2015-01-13 19:43:56      阅读:364      评论:0      收藏:0      [点我收藏+]

标签:

Flex端利用new PNGEncoder().encode(bitmapData)将png图片转换为ByteArray,然后传给服务器,服务端需要定义一个public ByteArray UploadImage(ByteArray ba)方法

服务器端相关代码

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Web;
using FluorineFx;
using FluorineFx.AMF3;

namespace ServiceLibrary5
{
    [RemotingService]
    public class ByteStreamService
    {
        public ByteArray UploadImage(ByteArray ba)
        {
            MemoryStream ms = new MemoryStream(ba.GetBuffer());
            Image img = Bitmap.FromStream(ms);

            Bitmap newImage = new Bitmap(img);

            MemoryStream tempStream = new MemoryStream();
            newImage.Save(tempStream, System.Drawing.Imaging.ImageFormat.Png);
            string path = HttpContext.Current.Server.MapPath("UpLoad/ByteArray.png");
            FileStream fs = new FileStream(path, FileMode.Create);
            tempStream.WriteTo(fs);
            fs.Close();

            ByteArray result = new ByteArray(tempStream);
            return result;
        }
    }
}

Flex端相关代码

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	<mx:Panel x="28" y="58" width="364" height="417" layout="absolute">
		<mx:Canvas id="myCanvas" x="0" y="0" width="334" height="331" 
			mouseDown="onMouseDown()" mouseUp="onMouseUp()" mouseMove="onMouseMove()">
		</mx:Canvas>
		<mx:ControlBar x="135" y="459">
			<mx:ColorPicker change="curColor = event.target.selectedColor" />
			<mx:Button label="清空" click="onClear()" />
			<mx:Button label="保存" click="onSave()" />
		</mx:ControlBar>
	</mx:Panel>
	<mx:Image id="imgBox" x="462" y="83"/>
	<mx:Script>
		<![CDATA[
			import mx.core.UIComponent;
			import mx.containers.Box;
			import mx.controls.Alert;
			import mx.rpc.events.ResultEvent;
			import mx.graphics.codec.PNGEncoder;
			private var flag:Boolean = false;
			private var startX:int;
			private var startY:int;
			private var curColor:uint = 0x000000;
		
			private function onSave():void
			{
				var bd:BitmapData = new BitmapData(myCanvas.width,myCanvas.height);
				bd.draw(myCanvas);
				var byteArray:ByteArray = new PNGEncoder().encode(bd);
				
				var nc:NetConnection = new NetConnection();
				nc.client = this;
				var responder:Responder = new Responder(onResult,onStatus);
				nc.connect("http://localhost:9782/Web/Gateway.aspx");
				nc.call("ServiceLibrary5.ByteStreamService.UploadImage",responder,byteArray);
			}
			
			private function onResult(result:ByteArray):void
			{
				//利用Loader读取图片
				var loader:Loader = new Loader();
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
				loader.loadBytes(result);
			}
			
			private function onComplete(event:Event):void
			{
				//利用UIComponent加载Loader对象
				var loader:Loader = (event.target  as  LoaderInfo).loader;
				loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onComplete);
	            			var pictureHolder:UIComponent = new UIComponent();
				pictureHolder.addChild(loader);
				this.imgBox.width = myCanvas.width;
				this.imgBox.height = myCanvas.height;
				this.imgBox.addChild(pictureHolder);
			}
			
			private function onStatus(obj:Object):void
			{
				Alert.show(obj.code.toString());
			}
		
			private function onClear():void
			{
				myCanvas.graphics.clear();
			}
		
			private function onMouseDown():void
			{
				startX = myCanvas.mouseX;
				startY = myCanvas.mouseY;
				flag = true;
			}
			
			private function onMouseUp():void
			{
				flag = false;
			}
			
			private function onMouseMove():void
			{
				if(flag)
				{
					myCanvas.graphics.moveTo(startX,startY);
					myCanvas.graphics.lineStyle(2,curColor);
					myCanvas.graphics.lineTo(myCanvas.mouseX,myCanvas.mouseY);
					
					startX = myCanvas.mouseX;
					startY = myCanvas.mouseY;
				}
			}
		]]>
	</mx:Script>
</mx:Application>

利用FluorineFx的ByteArray上传图片

标签:

原文地址:http://www.cnblogs.com/steven9801/p/4222238.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!