怎么显示Metafile类型的文件 类似于wmf

鲁虾 2009-03-19 05:11:00
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Metafile vMetafile = new Metafile(@"d:\a.wmf");
Graphics vGraphics = pictureBox1.CreateGraphics();
vGraphics.DrawImage(vMetafile, 0, 0, pictureBox1.Width, pictureBox1.Height);
vGraphics.Dispose();

}
怎么只显示一次,就不显示了,大家看怎么显示它!!
...全文
390 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
mickeydream 2009-04-09
  • 打赏
  • 举报
回复
建议LZ看看wmf文件结构,就可以按照自己的想法显示了,比如输出vrml,flash都可以,呵呵
txt_paul 2009-04-09
  • 打赏
  • 举报
回复
当再次引发 paint 事件时,就可以执行了。
友情一顶。
netharry 2009-04-09
  • 打赏
  • 举报
回复
另外也看看我的问题,如果这个问题解决了,矢量图就算差不多了:

http://topic.csdn.net/u/20090409/14/948593bc-db9b-4d19-acd0-a81a8ab540b2.html
netharry 2009-04-09
  • 打赏
  • 举报
回复
最近也正好研究矢量图:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

// for Marshal.Copy
using System.Runtime.InteropServices;

public class Form1 : Form
{
private Metafile metafile1;
private Graphics.EnumerateMetafileProc metafileDelegate;
private Point destPoint;
public Form1()
{
metafile1 = new Metafile(@"C:\Test.wmf");//emf,emf+都可以
metafileDelegate = new Graphics.EnumerateMetafileProc(MetafileCallback);
destPoint = new Point(20, 10);
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.EnumerateMetafile(metafile1, destPoint, metafileDelegate);
}
private bool MetafileCallback(
EmfPlusRecordType recordType,
int flags,
int dataSize,
IntPtr data,
PlayRecordCallback callbackData)
{
byte[] dataArray = null;
if (data != IntPtr.Zero)
{
// Copy the unmanaged record to a managed byte buffer
// that can be used by PlayRecord.
dataArray = new byte[dataSize];
Marshal.Copy(data, dataArray, 0, dataSize);
}

metafile1.PlayRecord(recordType, flags, dataSize, dataArray);

return true;
}

static void Main()
{
Application.Run(new Form1());
}
}

鲁虾 2009-04-09
  • 打赏
  • 举报
回复
目前想显示 AutoCAD 2005 谁做过???
鲁虾 2009-03-20
  • 打赏
  • 举报
回复
但怎么只显示一次,不显示了!!
Teng_s2000 2009-03-20
  • 打赏
  • 举报
回复

using(Graphics vGraphics = pictureBox1.CreateGraphics())
{
Metafile vMetafile = new Metafile(@"d:\a.wmf");
vGraphics.DrawImage(vMetafile, 0, 0, pictureBox1.Width, pictureBox1.Height);

}

让他自动Dispose看看呢
zzxap 2009-03-20
  • 打赏
  • 举报
回复
//vGraphics.Dispose();

zzxap 2009-03-20
  • 打赏
  • 举报
回复
unit Metaform;

interface

uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, Buttons, ExtCtrls;

type
TForm1 = class(TForm)
Panel1: TPanel;
BitBtn1: TBitBtn;
Image1: TImage;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

type
TMetafileCanvas = class(TCanvas)
private
FClipboardHandle: THandle;
FMetafileHandle: HMetafile;
FRect: TRect;
protected
procedure CreateHandle; override;
function GetMetafileHandle: HMetafile;
public
constructor Create;
destructor Destroy; override;
property Rect: TRect read FRect write FRect;
property MetafileHandle: HMetafile read GetMetafileHandle;
end;

constructor TMetafileCanvas.Create;
begin
inherited Create;
FClipboardHandle := GlobalAlloc(
GMEM_SHARE or GMEM_ZEROINIT, SizeOf(TMetafilePict));
end;

destructor TMetafileCanvas.Destroy;
begin
DeleteMetafile(CloseMetafile(Handle));
if Bool(FClipboardHandle) then GlobalFree(FClipboardHandle);
if Bool(FMetafileHandle) then DeleteMetafile(FMetafileHandle);
inherited Destroy;
end;

procedure TMetafileCanvas.CreateHandle;
var
MetafileDC: HDC;
begin
{ Create a metafile DC in memory }
MetafileDC := CreateMetaFile(nil);
if Bool(MetafileDC) then
begin
{ Map the top,left corner of the displayed rectangle to the
top,left of the
device context. Leave a border of 10 logical units around the
picture. }
with FRect do SetWindowOrg(MetafileDC, Left - 10, Top - 10);
{ Set the extent of the picture with a border of 10 logical units.
}
with FRect do SetWindowExt(MetafileDC, Right - Left + 20, Bottom -
Top + 20);
{ Play any valid metafile contents to it. }
if Bool(FMetafileHandle) then
begin
PlayMetafile(MetafileDC, FMetafileHandle);
end;
end;
Handle := MetafileDC;
end;

function TMetafileCanvas.GetMetafileHandle: HMetafile;
var
MetafilePict: PMetafilePict;
IC: HDC;
ExtRect: TRect;
begin
if Bool(FMetafileHandle) then DeleteMetafile(FMetafileHandle);
FMetafileHandle := CloseMetafile(Handle);
Handle := 0;
{ Prepair metafile for clipboard display. }
MetafilePict := GlobalLock(FClipboardHandle);
MetafilePict^.mm := mm_AnIsoTropic;
IC := CreateIC('DISPLAY', nil, nil, nil);
SetMapMode(IC, mm_HiMetric);
ExtRect := FRect;
DPtoLP(IC, ExtRect, 2);
DeleteDC(IC);
MetafilePict^.xExt := ExtRect.Right - ExtRect.Left;
MetafilePict^.yExt := ExtRect.Top - ExtRect.Bottom;
MetafilePict^.HMF := FMetafileHandle;
GlobalUnlock(FClipboardHandle);
{ I'm giving you this handle, but please do NOT eat it. }
Result := FClipboardHandle;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
MetafileCanvas : TMetafileCanvas;
begin
MetafileCanvas := TMetafileCanvas.Create;
MetafileCanvas.Rect := Rect(0,0,500,500);
MetafileCanvas.Ellipse(10,10,400,400);
Image1.Picture.Metafile.LoadFromClipboardFormat(
cf_MetafilePict, MetafileCanvas.MetafileHandle, 0);
MetafileCanvas.Free;
end;

end.
sxmonsy 2009-03-20
  • 打赏
  • 举报
回复
vGraphics.Dispose(); 都释放了还怎么再显示?
sxmonsy 2009-03-20
  • 打赏
  • 举报
回复
1楼的沙发太专业了
EveryCase 2009-03-20
  • 打赏
  • 举报
回复
顶~~~~~~~~~~~~~~
鲁虾 2009-03-20
  • 打赏
  • 举报
回复
没问回答?

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

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

试试用AI创作助手写篇文章吧