プロが教える店舗&オフィスのセキュリティ対策術

いつもお世話になっています。
「C#によるプログラミングWindows上」の第14章のP.764に書いてあるサンプルプログラムのImageDropにおいてうまく動かないのでわかる方お願いします。
サンプルプログラム自体はいくつも継承しているので全ては載せれないので最後の方に一部を載せておきます。
やっていることは、同じプログラムの2つのインスタンスを起動させクライアント領域に表示している画像をもう一つのインスタンスへドラッグアンドドロップさせて動作確認をしていましたら、以下のようなエラーがでました。
エラーが起きているのはDrawImage(...)メソッドの内部です。コールスタックを見ると内部でRealProxy()というメソッドを呼んでいるみたいですが、エラーもそれに関係しているようです。
わかる方お願いします。足りない情報等ありましたら、指摘していただければ載せますので。

<<環境>>
Vista
VisualStudio2005 SP1

<<エラーメッセージ(抜粋)>>
System.Runtime.Remoting.RemotingException はハンドルされませんでした。
Message="このリモート処理プロキシには、チャネル シンクがありません。待機中のサーバー チャネルが登録されていないか、またはこのアプリケーションに、サーバーと通信する適切なクライアント チャネルがありません。"
Source="mscorlib"
StackTrace:
場所 System.Runtime.Remoting.Proxies.RemotingProxy.InternalInvoke(IMethodCallMessage reqMcmMsg, Boolean useDispatchMessage, Int32 callType)
場所 System.Runtime.Remoting.Proxies.RemotingProxy.Invoke(IMessage reqMsg)
場所 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
場所 System.Object.FieldGetter(String typeName, String fieldName, Object& val)
場所 System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y)


<<ソース抜粋>>
namespace Chapter14
{
class ImageDrop : ImageClip
{



protected override void OnDragDrop(DragEventArgs drgevent)
{
base.OnDragDrop(drgevent);

if (drgevent.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] astr = (string[])drgevent.Data.GetData(DataFormats.FileDrop);

try
{
image = Image.FromFile(astr[0]);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, this.Text);
return;
}
strFileName = astr[0];
this.Text = strProgName + " - " + Path.GetFileName(strFileName);
this.Invalidate();
}
else
{
if (drgevent.Data.GetDataPresent(typeof(Metafile)))
{
image = (Image)drgevent.Data.GetData(typeof(Metafile));
}
else if (drgevent.Data.GetDataPresent(typeof(Bitmap)))
{
image = (Image)drgevent.Data.GetData(typeof(Bitmap));
}

bIsTarget = true;
strFileName = "DragAndDrop";
this.Text = strProgName + " - " + strFileName;
this.Invalidate();
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);

if (image != null)
{
bIsTarget = false;

DragDropEffects dde = DoDragDrop(image, DragDropEffects.Copy | DragDropEffects.Move);
if (dde == DragDropEffects.Move && !bIsTarget)
{
image = null;
}
}
}

}

((別クラス内で[ImageDropの継承元]))
namespace Chapter09
{
public partial class ImageOpen : Form
{



protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);

Graphics gr = e.Graphics;

if (image != null)
{
gr.DrawImage(image, 0, 0);
}
}
}

A 回答 (1件)

cs2003/2005双方ともにこの現象は起きるようですね



別な方法として DataObjectを使ってファイル名とイメージを送る方法ならうまくいくようですよ
ファイルをドロップされた際に BMPやJPGなどで保存して置きます
その保存されたイメージをつかってDoDragDropを行います

// TempImage.jpgで保存されている仮定
string[] ss = { "TempImage.jpg" };
FileInfo oFInfo = new FileInfo( ss[0] );
ss[0] = oFInfo.FullName;

DataObject dObject = new DataObject( DataFormats.FileDrop, ss );
dObject.SetData( DataFormats.Bitmap, (Bitmap)Image.FromFile( ss[0] );
DragDropEffects dde = DoDragDrop( dObject, DragDropEffects.All );
といった具合です …

この回答への補足

回答ありがとうございます。
そうですか、C#2003でも同じ現象がおきるのですか・・・
そして、代替案まで出していただき誠にありがとうございます。

実際に書き換えてやってみます。そのあと改めてお礼を書きたいと思います。

補足日時:2008/02/19 18:06
    • good
    • 0
この回答へのお礼

おかげさまで、動かすことができました。
以下のように書き換えましたら動作しました。
なお、スマートではないやり方が入ってますが、参考にされる方は気を付けてください。

namespace Chapter14
{
class ImageDrop : ImageClip
{



protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);

if (image != null)
{
bIsTarget = false;
string[] strTempFile = { Directory.GetCurrentDirectory() + "\\TempImage.jpg" };
int num = 0;
while (File.Exists(strTempFile[0]))
{
strTempFile[0] += num.ToString();
}
image.Save(strTempFile[0]);

DataObject data = new DataObject();
data.SetData(DataFormats.FileDrop, strTempFile);
data.SetData(DataFormats.Bitmap, (Bitmap)Image.FromFile(strTempFile[0]));
DragDropEffects dde = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Move);
//DragDropEffects dde = DoDragDrop(image, DragDropEffects.Copy | DragDropEffects.Move);
if (dde == DragDropEffects.Move && !bIsTarget)
{
image = null;
this.Invalidate();
}
}
}

}

お礼日時:2008/02/19 19:59

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!