• ベストアンサー

C#でフォームのコピーをしたい

最近C#を始めたのですが、フォームをコピーする際に Bitmap myBmp = new Bitmap(this.Width, this.Height); this.DrawToBitmap(myBmp, new Rectangle(0, 0, this.Width, this.Height)); Clipboard.SetDataObject(myBmp, false); といった風にコピーをしていたのですが、これではフォーム全体がコピーされ、縁ができてしまいます。範囲指定してフォームコピーをする方法はないでしょうか。

質問者が選んだベストアンサー

  • ベストアンサー
回答No.1

 こんばんは。  クライアント領域のスクリーンショットを取りたいのでしょうか。  DrawToBitmap()を呼ぶとフォームの外枠も含まれるので、もう一つ用意しておいたビットマップに向かって、外枠の分だけずらして転送します。  後はクリップボードに向かって送ってあげれば、外枠を抜いたスクリーンショットが取れているはずです。  以下参考程度に。 //フォームからスクリーンショットを撮る  Bitmap myBmp = new Bitmap(this.Width, this.Height); this.DrawToBitmap(myBmp, new Rectangle(0, 0, this.Width, this.Height)); //外枠を除外されたスクリーンショット Bitmap sendBmp = new Bitmap(this.ClientSize.Width, this.ClientSize.Height); //外枠を除外して転送 Graphics gfx = Graphics.FromImage(sendBmp); gfx.DrawImage(myBmp, this.PointToClient(this.Location)); //クリップボードに送る Clipboard.SetDataObject(sendBmp, true); //後始末 sendBmp.Dispose(); myBmp.Dispose();

tynxub
質問者

お礼

的確な回答ありがとうございます。 困っていた問題が解決しました。 もっと精進していきたいと思います。

関連するQ&A

  • C#で画像をクリップボードへのコピーと貼り付けについて

    GraphicsオブジェクトのDrawImageメソッドを使って画像を表示させています。 この画像をクリップボードへコピーしてほかのところに貼り付けしたいのですが、クリップボードへはClipboard.SetDataObject()を使用すればよいと思っています。 しかし、どのように使ったらいいのかよくわかりません。現在のソースは以下のようになっています。 //Bitmapオブジェクトの作成(画像ファイルを読み込む) Bitmap bmap = new Bitmap(@"C:\test2.bmp"); //元の画像からの切り取り範囲の指定 RectangleF rectSrc = new RectangleF                 (pointX,pointY,WidthX,WidthY); //貼り付け先を指定範囲の指定 RectangleF rectDst = new RectangleF(0, 0, DstX,DstY); //PictureBox1のGraphicsオブジェクトの作成 Graphics g= pictureBox1.CreateGraphics(); //画像の描画 g.DrawImage(bmap, rectDst,rectSrc,GraphicsUnit.Pixel); //クリップボードへのコピー Clipboard.SetDataObject(g); よろしくおねがいいたします。

  • Rectangle(Bitmap)の幅と高さ

     以下、VBの記法で書きます。Windows10,.NETのGDI+を使用しています。  今まであまり気にしていなかったのですが、今回LockBitsとUnLockBitsを使用してBitmapのpixel情報をCopyするコードを書く事になり、急に気になり出しました。  LockBitsを使用する際、Rectangle構造体で読みだすBitmapの範囲を指定します。Rectangleが指定する実際のpixelですが、   (Rectangle.Left + Rectangle.Width - 1)×(Rectangle.Top + Rectangle.Height - 1) の範囲かな?、と思いました。例えば、   Dim Rect as Rectangle = New Rectangle(1, 1, 0, 0) などとやると、Rectは空になるし、(1, 1)一点を表すRectangleなら、   Dim Rect as Rectangle = New Rectangle(1, 1, 1, 1) なので・・・。  BitmapのWidthとHeightについても同様かと思います。実際の動作には余り影響しないのですが、気になったもので・・・。

  • 実行時に、フォームを印刷

    WindowsXP, vb.net2005で開発しております。 プログラム実行時にフォームを印刷したいと考えております。 フォーム上には下記の3種類のコントロールが貼り付けてあります。  ラベル、  picturebox(イメージを表示)、  panel(バーコードを表示) 印刷はPrintDocumentを使用しています。 フォームをキャプチャーし、画像を印刷するようにしています。 印刷を行うと、必ずpictureboxの画像が最前面に表示されてしまいます。 フォームに指定した重なり順のまま印刷するにはどうすればよいのでしょうか? 以下、PrintDocument のPrintPageのソースです。 Private Sub pd_PrintPage(  ByVal sender As System.Object,  ByVal e As System.Drawing.Printing.PrintPageEventArgs)  Handles pd.PrintPage  'コントロールの外観を描画するBitmapの作成  Dim bmp As New Bitmap(Me.Size.Width, Me.Size.Height)  'キャプチャする  Me.DrawToBitmap(bmp,          New Rectangle(0, 0, Me.Size.Width,          Me.Size.Height))  '画像を描画する  e.Graphics.DrawImage(bmp, 0, 0, e.PageBounds.Width,            e.PageBounds.Height)  '後始末  bmp.Dispose()  '次のページがないことを通知する  e.HasMorePages = False End Sub

  • ユーザー定義オブジェクト

    現在、ユーザー定義オブジェクトについて勉強しています。 以下は本からの抜粋です。 //コンストラクタを定義する。 //thisで参照するオブジェクトの初期化法に注目   function Rectangle(w,h) { this.width=w; this.height=h; } //コンストラクタを呼び出して2つの長方形オブジェクトを生成する。 //widthとheightをコンストラクタに渡して、それぞれの新しい //オブジェクトを適切な形で初期化するやり方に注目 var rect1=new Rectangle(2,4); var rect2=new Rectangle(8.5,11); ★質問★ this.width=w; this.height=h; var rect1=new Rectangle(2,4); var rect2=new Rectangle(8.5,11); この2箇所で行っている「初期化」という作業の意味がよく分かりません。 また、ユーザー定義オブジェクトの概念についていまいちイメージ がつかめていません(特にthisとnewを使用している意図など) ご教授いただきたく、よろしくお願いします。

  • Javaの基礎のプログラム

    結城浩さんの本に載っていたプログラムです。 ですが、説明がわかりにくくていまいち処理がわからなかったので 質問させていただきました。 どうやら、X軸とY軸がありY軸は下に行けばいくほど数字が大きくなる。 X軸は右に行けばいくほど大きくなる。座標上にある、二つの長方形の重なりあう部分の座標を求めるプログラムのようです。(イメージとしては、下に添付してある画像のような感じになるようです。) 質問としては一つです。 プログラム中のこの部分なのですが、おそらく「長方形の座標を示している」行であると思います。 a = new Rectangle(0, 0, 20, 10); b = new Rectangle(5, 5, 20, 10); c = new Rectangle(20, 10, 20, 10); d = new Rectangle(-10, -20, 100, 200); e = new Rectangle(21, 11, 20, 10); その後の処理としては、aとb→c→d→eというようにそれぞれ比較していっているのもなんとなくわかります。 ですが、しっかり解説されていなかったので、この4つの数字がどのように座標を示しているのかよくわかりませんでした。 よろしくお願いします。 class Rectangle { final int INITIAL_WIDTH = 10; final int INITIAL_HEIGHT = 20; int width; int height; int x; int y; Rectangle() { width = INITIAL_WIDTH; height = INITIAL_HEIGHT; x = 0; y = 0; } Rectangle(int width, int height) { this.width = width; this.height = height; this.x = 0; this.y = 0; } Rectangle(int x, int y, int width, int height) { this.width = width; this.height = height; this.x = x; this.y = y; } void setLocation(int x, int y) { this.x = x; this.y = y; } void setSize(int width, int height) { this.width = width; this.height = height; } public String toString() { return "[" + x + ", " + y + ", " + width + ", " + height + "]"; } Rectangle intersect(Rectangle r) { int sx = Math.max(this.x, r.x); int sy = Math.max(this.y, r.y); int ex = Math.min(this.x + this.width, r.x + r.width); int ey = Math.min(this.y + this.height, r.y + r.height); int newwidth = ex - sx; int newheight = ey - sy; if (newwidth > 0 && newheight > 0) { return new Rectangle(sx, sy, newwidth, newheight); } else { return null; } } public static void main(String[] args) { Rectangle a, b, c, d, e; a = new Rectangle(0, 0, 20, 10); b = new Rectangle(5, 5, 20, 10); c = new Rectangle(20, 10, 20, 10); d = new Rectangle(-10, -20, 100, 200); e = new Rectangle(21, 11, 20, 10); System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); System.out.println("e = " + e); System.out.println("a と a の重なり = " + a.intersect(a)); System.out.println("a と b の重なり = " + a.intersect(b)); System.out.println("a と c の重なり = " + a.intersect(c)); System.out.println("a と d の重なり = " + a.intersect(d)); System.out.println("a と e の重なり = " + a.intersect(e)); } }

    • ベストアンサー
    • Java
  • ★他アプリの画面一部をコピー表示したい

    お世話になります。 さて図のように「アプリX」があって、 その画面の一部を自作ソフト「Me」画面に表示したいのです。 (WindowsでVBで作成中) いろいろ調べると、 http://dobon.net/vb/dotnet/graphics/invokepaint.html に以下のようなプログラムがありました。 'コントロールの外観を描画するBitmapの作成 Dim bmp As New Bitmap(Me.Width, Me.Height) 'キャプチャする Me.DrawToBitmap(bmp, New Rectangle(0, 0, Me.Width, Me.Height)) 'ファイルに保存する bmp.Save("C:\test\1.png") '後始末 bmp.Dispose() しかしこれはMe自身の画面であって、意味がありません。 それにファイルで保存ではなくMeに表示したいのです。 (これは多分できると思います) 最大の問題は、「アプリX」の“フォーム名”です。 http://dobon.net/vb/dotnet/form/showintaskbar.html には、「タスクバーにフォームを表示しないようにする」として、 (これもやりたいことの一つですが) 'Form1のインスタンスを作成 Dim f As New Form1() 'タスクバーに表示しない f.ShowInTaskbar = False 'フォームを表示する f.Show() とありますが、「Form1」をどうやって取得するのか書いていません。 つまり「アプリX」のフォーム名が分からないのです。 それをどうやって知ることができるでしょうか? それとも考え方が根本的におかしい? よろしくお願い申し上げます m(__)m

  • ユーザーコントロールのDrawToBitmap

    vb.net(VisualStudio2015)で作成したユーザーコントロール内の画面を「DrawToBitmap」で取得しフォーム内に表示したいのですが上手く表示できません。もしご存知の方がいましたらご教授して頂けますと助かります。宜しくお願い致します。 Private SecondScreen as New UserControl1 'ユーザーコントロール内の画面を取得 Dim mBitmap as New Bitmap(SecondScreen.Width,SecondScreen.Height) SecondScreen.DrawToBitmap(mBitmap,New Rectangle(0,0,SecondScreen.Width,SecondScreen.Height)) . . . '取得した画面をフォームに表示 Dim g as Graphics = e.Graphics g.DrawImage(mBitmap,new Rectangle(0,0,SecondScreen.Width,SecondScreen.Height)) いくつか省略箇所がありますが、上記の方法ですとユーザーコントロール内の画面が取得できなく、「SecondScreen.Visible=True」で一度表示させてから取得すると表示されます。 フォーム内にユーザーコントロールを張り付けて表示させるのではなく、一度「DrawToBitmap」で取得させてから表示させたいのです。 お手数ですが、ご教授お待ちしています。 宜しくお願い致します。

  • C#のGraphicsクラスについてです。

    C#のGraphicsクラスを用いて画像をフェードインで表示させよとしています。 まず以下のコードをごらんください。 ただ、フェードインで画像は表示されるものの、フォームの操作が一切できなくなってしまいます。 そのためマルチスレッドにしようとしたのですが using System; using System.IO; using System.Windows.Forms; using System.Drawing; using System.Web; using System.Net; using System.Text; using System.Threading; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Diagnostics; public class MainClass{ public static void Main(string [] args){ NewForm formObj = new NewForm(); Application .Run(formObj); } } public class NewForm : Form { //インスタンス変数の宣言 public Graphics g ; public Bitmap mapObj ; public Image imageObj; public ImageAttributes ia; public ColorMatrix cm; public Thread th ; public ParameterizedThreadStart ts; public PaintEventArgs e; public Rectangle rec; public int flag = 0; public delegate void TestDelegate(); public TestDelegate deleObj; public NewForm(){ Button buttonObj = new Button(); buttonObj.Width=100; buttonObj.Height = 30; //フェードさせるためのイベント発行用ボタンの設置 buttonObj.Click += new EventHandler(this.SetMethod); this.Controls.Add(buttonObj); } public void SetMethod(object sender , EventArgs e){ this.Paint += new PaintEventHandler(this.ThreadMethod); //フォームコントロールの再描画を促す this.Invalidate(); } public void ThreadMethod(object sender ,PaintEventArgs eventObj){ this.ts = new ParameterizedThreadStart(this.ThreadRenderMethod); this.th = new Thread(this.ts); this.th.Start(eventObj); MessageBox.Show("ThreadMethod実行後"); MessageBox.Show(InvokeRequired.ToString()); this.th.Join(); } public void ThreadRenderMethod(object paintObj){ MessageBox.Show(InvokeRequired.ToString()); this.deleObj =delegate(){ //無現ループしてしまうので、再描画イベント後イベントハンドラーを削除 this.Paint -= new PaintEventHandler(this.ThreadMethod); PaintEventArgs e = (PaintEventArgs)paintObj; try{ Console.WriteLine("paint メソッド発生"); this. g = e.Graphics; this.mapObj = new Bitmap(this.Width,this.Height); this.imageObj = Image.FromFile("C:\\c#\\test.jpg"); //this.g = Graphics.FromImage(this.mapObj); this.cm = new ColorMatrix(); this.cm.Matrix00 = 1; this.cm.Matrix11 = 1; this.cm.Matrix22 = 1; this.cm.Matrix33 = 0.0F; this.cm.Matrix44 = 1; this.ia = new ImageAttributes(); this.ia.SetColorMatrix(this.cm); this.rec = new Rectangle(0, 0, this.Width, this.Height); this.g.DrawImage(this.imageObj,rec,0,0,this.imageObj.Width,imageObj.Height,GraphicsUnit.Pixel,this.ia); this.BackgroundImage = mapObj; for(double i = 0.0; i <= 1.0; i = i + 0.001){ this.cm.Matrix33 = (float) i; this.ia.SetColorMatrix(this.cm); this.g.DrawImage(this.imageObj,rec,0,0,this.imageObj.Width,imageObj.Height,GraphicsUnit.Pixel,this.ia); this.BackgroundImage = this.mapObj; Thread.Sleep(100); } //this.imageObj.Dispose(); //this.g.Dispose(); }catch(Exception ex){ Console.WriteLine(ex.ToString()); } Console.WriteLine("paint end"); }; this.deleObj(); //this.Invoke(this.deleObj); this.BackgroundImage = Image.FromFile("C:\\c#\\test.jpg"); } } 上記コードでも、フェードは動作するものの、やはりフォームの操作ができなくなります。 どう対処したらよいでしょうか? 識者の方、よろしくご教授ください お願いいたします

  • パッケージ化とコンパイルについて

    下記のプログラムにについての質問になります。 /*Rectangle.java*/ package com.shoeisha.shape; public class Rectangle { private int width; private int height; public Rectangle() { } public Rectangle(int width, int height) { this.width = width; this.height = height; } public int getArea(){ return width * height; } } /*Test.java*/ import com.shoeisha.shape.*; ^^(1) class Test { public static void main(String[] args) { Rectangle rc = new Rectangle(10, 15); System.out.println(rc.getArea()); } } (1)の箇所になりますが、com.shoeisha.shapeにあるクラス(Rectangle)を 利用し、プログラムを作成しようと思うのですが、Test.javaにてコンパイルエラーが生じてしまいます。 (1)の箇所をRectangleにするとコンパイルが通るようになり、「*」は使えないものかと思えてしまいます。 java.util.*;といった、既に用意されているものは普通に使えるのですが、自作でパッケージ化した物は 正規表現を使うことができないものなんでしょうか?? 何かの設定がされていないから、正規表現が使えないのではと思っておりますが、 それ以上がわかりませんでしたので、掲示板にて記載させていただきました。 以上、ご教授の程お願い致します。

    • ベストアンサー
    • Java
  • VB2010で、キャプチャしたものを貼り付けるにはどうしたらいいのでし

    VB2010で、キャプチャしたものを貼り付けるにはどうしたらいいのでしょうか。 ご存知の方教えてください。よろしくお願いします。 ???の部分のコードがわかりません。 'キャプチャする Dim copy_bmp As New Bitmap(Me.PictureBox1.Width, Me.PictureBox1.Height) Me.DrawToBitmap(copy_bmp, New Rectangle(0, 0, Me.PictureBox1.Width, Me.PictureBox1.Height)) PictureBox3 = copy_bmp??? copy_bmp.Dispose()