• 全部
  • C#综合技术
  • C#互联网桌面应用
  • AppLauncher
  • WinForm&WPF
  • C#开发新技术
  • 问答

100分求解 关于二进制序列化和反序列化的问题! 非常棘手! 在线等待

stardicky 2004-09-27 04:32:32
[Serializable]
public class DImage
{
private Image m_Image;
}

如何把 Image 字段里的内容也保存在序列化后的文件里呀?
...全文
148 点赞 收藏 10
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Jim3 2004-09-27
直接序列化,反序列化就可以啊

你有什么问题?

(Image本身可以序列化,所以不需特别的处理,
一些特殊的数据需要序列化,可以实现接口ISerializable
你这个就不需要了)
回复
nerk 2004-09-27
DImage img = new DImage(System.Drawing.Image.FromFile(Server.MapPath("images/b.bmp")), Color.Red);
BinaryFormatter f = new BinaryFormatter();
System.IO.MemoryStream stream = new System.IO.MemoryStream();
f.Serialize(stream, img);

//不可以么?
回复
fim 2004-09-27
if(m_Image != null)写错了,应该是DImage1了

ps:我写的语法可能有点问题,我不会C#,将就着看吧
回复
fim 2004-09-27
public Stream s;
public BinaryFormatter f;



//序列化
if(m_Image != null)
{
s = File.Create(@"c:\test.dat");
f = new BinaryFormatter();
f.Serialize(s, DImage1);//DImage1为你要序列化的类实例
s.Close();
}

//反序列化
s = File.OpenRead(@"c:\test.dat");
f = new BinaryFormatter();
DImage DImage2 = (DImage)f.Deserialize(s);
//DImage2从这里就可以用了
s.Close();
回复
stardicky 2004-09-27
如何把 DImage 这个类的实例进行序列化!
回复
stardicky 2004-09-27
using System;
using System.Drawing;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace FlyerDesign
{
/// <summary>
/// DImage 的摘要说明。
/// </summary>
[Serializable]
public class DImage : DShape
{
private Image m_Image;
public DImage(Image OpenedImage, Color penColor)
{
this.m_Image = OpenedImage;
this.bounding = new Rectangle(new Point(0,0),this.m_Image.Size);
this.penColor = penColor;
RefrushHandsLocation();
}

public override void ChangeSize(System.Windows.Forms.Cursor CursorState, System.Drawing.Point p)
{
if(CursorState == System.Windows.Forms.Cursors.SizeAll)
{
Point Center = new Point(this.bounding.Left + (int)(this.bounding.Size.Width / 2),this.bounding.Top + (int)(this.bounding.Size.Height / 2));
this.bounding.Offset(p.X - Center.X,p.Y - Center.Y);
}
else if((CursorState == System.Windows.Forms.Cursors.SizeNESW) || (CursorState == System.Windows.Forms.Cursors.SizeNWSE))
{
if(CurrentHande.Equals(this.ActiveHands[0])) //左上角
this.bounding = new Rectangle(p,new Size((int)Math.Abs((int)(this.ActiveHands[2].X - p.X)) , (int)Math.Abs((int)(this.ActiveHands[2].Y - p.Y))));
else if(CurrentHande.Equals(this.ActiveHands[1]))//右上角
{
Point LeftTop = new Point(this.ActiveHands[3].X,p.Y);
int Width = (int)Math.Abs((int)(p.X - this.ActiveHands[3].X));
int Height = (int)Math.Abs((int)(this.ActiveHands[3].Y - p.Y));
this.bounding = new Rectangle(LeftTop,new Size(Width,Height));
}
else if(CurrentHande.Equals(this.ActiveHands[2]))//右下角
this.bounding = new Rectangle(this.ActiveHands[0],new Size(p.X - this.ActiveHands[0].X,p.Y - this.ActiveHands[0].Y));
else if(CurrentHande.Equals(this.ActiveHands[3]))//左下角
{
Point LeftTop = new Point(p.X,this.ActiveHands[1].Y);
int Width =(int)Math.Abs((int)(this.ActiveHands[1].X - p.X));
int Height = (int)Math.Abs((int)(p.Y - this.ActiveHands[1].Y));
this.bounding = new Rectangle(LeftTop,new Size(Width,Height));
}
CurrentHande = p;
}//end of elseif
RefrushHandsLocation();
}

public override System.Windows.Forms.Cursor GetCursorMode(System.Drawing.Point p)
{
foreach(Point tmp in this.ActiveHands)
{
Region m_Region = new Region(new Rectangle(tmp.X - 2,tmp.Y - 2,4,4));
if(m_Region.IsVisible(p))
{
if(tmp.Equals(this.ActiveHands[0]))
{
CurrentHande = this.ActiveHands[0];
return System.Windows.Forms.Cursors.SizeNWSE;
}
else if(tmp.Equals(this.ActiveHands[1]))
{
CurrentHande = this.ActiveHands[1];
return System.Windows.Forms.Cursors.SizeNESW;
}
else if(tmp.Equals(this.ActiveHands[2]))
{
CurrentHande = this.ActiveHands[2];
return System.Windows.Forms.Cursors.SizeNWSE;
}
else if(tmp.Equals(this.ActiveHands[3]))
{
CurrentHande = this.ActiveHands[3];
return System.Windows.Forms.Cursors.SizeNESW;
}
else if(tmp.Equals(this.ActiveHands[4]))
{
CurrentHande = this.ActiveHands[4];
return System.Windows.Forms.Cursors.SizeAll;
}
}
}
return System.Windows.Forms.Cursors.Default;
}

public override void RefrushHandsLocation()
{
Point LeftTop = new Point(this.bounding.Left,this.bounding.Top);
Point TopRight = new Point(this.bounding.Right,this.bounding.Top);
Point BottomRight = new Point(this.bounding.Right,this.bounding.Bottom);
Point LeftBottom = new Point(this.bounding.Left,this.bounding.Bottom);
Point Center = new Point(this.bounding.Left + (int)(this.bounding.Size.Width / 2),this.bounding.Top + (int)(this.bounding.Size.Height / 2));
this.ActiveHands = new Point[]{LeftTop,TopRight,BottomRight,LeftBottom,Center};
}

public override void Draw(System.Drawing.Graphics g)
{
using (Pen p = new Pen(penColor))
{
g.DrawImage(this.m_Image,this.bounding);
g.DrawRectangle(p,this.bounding);
}

if(this.m_Selected)
{
using(Pen p=new Pen(Color.Red))
{
foreach(Point tmp in this.ActiveHands)
{
g.DrawRectangle(p, tmp.X - 2, tmp.Y - 2, 4, 4);
}
}
}
}

public override Point CenterPoint
{
get
{
return this.ActiveHands[4];
}
set
{
this.ChangeSize(System.Windows.Forms.Cursors.SizeAll,value);
}
}

public override DShape Clone()
{
return (DShape)this.MemberwiseClone();
}
}
}


using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace FlyerDesign
{
/// <summary>
/// 图形抽象基类
/// </summary>
[Serializable]
public abstract class DShape
{
public abstract void Draw(Graphics g); //自身重画
protected Rectangle bounding; //图形所在区域
protected Color penColor; //图形常规颜色
protected Point CurrentHande = Point.Empty; //当前选择的操作柄
public abstract DShape Clone(); //创建当前对象的浅备份
public abstract Point CenterPoint
{
get;set;
}
public bool CanSelect(Point p) //是否能被选中
{
Region m_Region = new Region(this.bounding);

if(m_Region.IsVisible(p))
return true;
else
return false;
}

public bool CanSelect(Region r)
{
if(r.IsVisible(this.CenterPoint))
return true;
else
return false;
}

public Rectangle Rectangle
{
get
{
return this.bounding;
}
set
{
this.bounding = value;
RefrushHandsLocation();
}
}

public abstract System.Windows.Forms.Cursor GetCursorMode(Point p); //返回当前鼠标应该显示的模式
public abstract void ChangeSize(System.Windows.Forms.Cursor CursorState,Point p);


public Point[] ActiveHands; //操纵手柄的坐标

protected bool m_Selected; //图形设为选中状态
public bool Selected
{
get
{
return m_Selected;
}
set
{
m_Selected = value;
}
}

public abstract void RefrushHandsLocation(); //更新各个手柄的坐标


protected string name; //图形唯一ID
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}

public Color PenColor
{
get
{
return penColor;
}
set
{
penColor = value;
}
}
// 还应具有属性
// 还应具有移动、调整大小等方法。
}
}



回复
fim 2004-09-27
f.Serialize(s, "序列化图片");
这不是第二个吗,你也可以第三个啊
回复
stardicky 2004-09-27
这样导致了其他的对象,无法正常序列化!
回复
stardicky 2004-09-27
不行,我这个类里面还有其他需要序列化的对象!你这样只序列化了其中的一个!
回复
fim 2004-09-27
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

Stream s;
BinaryFormatter f;

if(m_Image != null)
{
s = File.Create("c:\test.dat");
f = new BinaryFormatter();
f.Serialize(s, m_Image);
f.Serialize(s, "序列化图片");
s.Close();
}
这样行不行?
回复
发帖
C#
创建于2007-09-28

10.5w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2004-09-27 04:32
社区公告

让您成为最强悍的C#开发者