VC#のGraphicsでBmpがにじむ

このQ&Aのポイント
  • VC#でビットマップのファイルを表示するプログラム を勉強中です。拡大表示する際に、ドットの輪郭がにじんで表示される問題が発生しました。
  • 元々のファイルをペイント等で開くとくっきりした正方形になっているのに、プログラムで拡大表示するとドットの輪郭がにじむ問題があります。
  • ビットマップの拡大表示プログラムをVC#で作成していますが、ドットの輪郭がにじむ現象が起きてしまいます。にじまないようにする方法を教えていただけないでしょうか。
回答を見る
  • ベストアンサー

VC#のGraphicsでBmpがにじむ

VC#でビットマップのファイルを表示するプログラムの勉強をしています。以下のようなコードを書いてみました。 Graphics mGraphics; Bitmap mBitmap; PictureBox mPictureBox; mGraphics = mPictureBox.CreateGraphics(); mBitmap = new Bitmap(file_name); int width = (int)(mBitmap.Width * mRatio); int height = (int)(mBitmap.Height * mRatio); mGraphics.DrawImage(mBitmap, 0, 0, width, height); このような感じで、拡大ボタンをクリックするとmRatioを大きくして拡大表示するようなものです。これで拡大表示自体はできたのですが、各ドット(ピクセル?)が確認できるほどに拡大すると、ドットの輪郭がにじんだように表示されます。もちろん、元々のファイルをペイント等で開いて拡大表示するとくっきりした正方形になっています。拡大表示してもにじまないようにする方法はあるのでしょうか。ご存じの方がいらっしゃったらご教授くださいますようにお願いします。

  • kary
  • お礼率95% (82/86)

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

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

スケーリングするときの補間モード mGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;

kary
質問者

お礼

ありがとうございます。思っていた通りにできました。他にもいろいろなモードがあるようなので勉強してみます。

関連するQ&A

  • C# Picturebox 縮小・拡大時の表示方法

    下記のようなコードで、画像の拡大・縮小を行っています。 ただ画像の縮小時、PictureBox内の余分な部分について、(1)縮小前の 画像が残像のように表示されています。 また通常、画像の拡大・縮小前の画像では画像サイズがPictureBoxよりも大きいサイズの 場合、スクロールバーを表示するようにしています。 (2)でも、画像拡大時、スクロールバーが出るには出ているのですが、画像全てを 確認できません。 (1)、(2)の問題について、解決方法をご存知の方、お願いです、、、教えてください。。。 かなり、困ってます。。。。 Bitmap img = new Bitmap(pictureBox1.Image.Tag.ToString()); //PictureBox1のGraphicsオブジェクトの作成 Graphics g = pictureBox1.CreateGraphics(); //拡大・もしくは縮小 RectangleF rect = new RectangleF(0, 0, zoom * img.Width, zoom * img.Height); g.DrawImage(img, rect); //BitmapとGraphicsオブジェクトを破棄 img.Dispose(); g.Dispose();

  • Open CVについて

    現在,OpenCVとVC++2008のC++/CLIでwindows formアプリケーションを作成しています。 フォーム上のpictureBoxにUSBカメラから取得した画像を,録画しながら,pictureBoxに表示させています。しかし,取り込んだ画像が,pictureBoxに収まり切っていないようです。(添付画像のようなに枠内の画像に対して,肌色部分しか,pictureBoxに表示されていない状態) コードは http://hisoap.jugem.jp/?eid=156 を参考に,以下のように記述しています。 CvCapture *capture srcImage = cvQueryFrame(capture); Bitmap^ bmpPicBox = gcnew Bitmap(pictureBox->Width, pictureBox->Height); pictureBox->Image = bmpPicBox; Graphics^g = Graphics::FromImage(pictureBox->Image); Bitmap^ bmp = gcnew Bitmap(pictureBox->Width, pictureBox->Height, srcImage->widthStep,System::Drawing::Imaging::PixelFormat::Format24bppRgb, IntPtr(srcImage->imageData)); g->DrawImage(bmp, 0, 0, srcImage->width , srcImage->height); どのようにすれば,画像をpictureBoxにぴったりと表示させることができるでしょうか?

  • 二つの画像を並べて表示

    ひとりで独学でVC++2005を勉強してるのですが、pictureBoxでつまずいます。初心者ですがわかりやすく教えて頂けたらと思います。 やりたい事はタイトル通り、画像を単純に並べて表示したいだけなのですが、WEB場で検索しても的を得た答えが見つかりませんでした。 二つの画像を読み込んで、新しいBitmapデータに書き込めばいいのかと思ったのですが、どうやっていいのか見当が付きません。 Bitmap^ bmpA = gcnew Bitmap("testAA.bmp"); Bitmap^ bmpB = gcnew Bitmap("testAB.bmp"); int w = bmpA->Width + bmpB->Width; int h = bmpA->Height + bmpB->Height; Bitmap^ bmp = gcnew Bitmap(w,h); //ここで何やっていいかわかりません。 pictureBox1->Image = bmp; 最終的にはタイル的にどんどん並べて行きたいと考えています。 助言の方よろしくお願いします。

  • VC#2010 アスペクト比を保った拡大縮小

    こんにちは。アスペクト比を保った拡大縮小について質問です。 [やりたい事] ・本を開いたようなイメージで、Panelの中に2個のPictureBoxを左右に配置しています。 ・Panelのサイズを拡大縮小した時に、画像の元のアスペクト比を保ったまま拡大縮小したい。 ・その際、PictureBox自身に自分の位置や大きさを計算させたい。 [現状と問題点] ・なんとなく動作しているが、画像がチラついてしまう。 ・PictureBoxのBackColorに色をつけているが、画像の端に微妙にその色が見えている時がある。 ・自分なりに考えて計算式を作ってみましたが、実はもっとスマートな計算方法があるなら教えて頂きたいです。 [プロジェクトアップロード先] http://www1.axfc.net/uploader/so/2808804 テスト用の画像も入っています。パスワードは設定していません。 実行すれば現状の動作を確認できるようになっています。 [ソースコード抜粋] ・・・ 問題の箇所 → mPictureBox.RefleshPosition()の中身 private void Form1_Load(object sender, EventArgs e) { //Formにpanelを追加 Panel panel1 = new Panel(); panel1.Dock = DockStyle.Fill; this.Controls.Add(panel1); //panelにpictureBoxを追加 panel1.Controls.Add(new mPictureBox(panel1, eSide.Left, @"..\..\800_600.jpg")); panel1.Controls.Add(new mPictureBox(panel1, eSide.Right, @"..\..\600_800.jpg")); panel1.Refresh(); } ----------------------------------------------------------- //thisはmPictureBoxです //this.sideはenum eSide{Left, Right} というenumです。 //RefleshPosition()はmPictureBoxのメソッドです。 //mPictureBoxのPaintイベントや親のSizeChangedで呼ばれます。 public void RefleshPosition() { if (this.Image.Height > this.Image.Width) { //元画像の高さ > 幅 の場合 //自分(PictureBox)の高さ = 親(Panel)の高さ this.Height = this.Parent.Height; //自分(PictureBox)の幅  = [元画像の幅] * ([自分の高さと元画像の高さの倍率]) this.Width = (int)(Image.Width * ((double)this.Height / (double)this.Image.Height)); this.Top = 0; //↑で自分の幅を計算した結果、1ページ分の幅より広くなってしまっていたら if (this.Width > (this.Parent.Width / 2.0)) { //自分(PictureBox)の幅 = 親(Panel)の幅 this.Width = (int)(this.Parent.Width / 2.0); //自分(PictureBox)の高さ= [元画像の高さ] * ([自分の幅と元画像の幅の倍率]) this.Height = (int)(Image.Height * ((double)this.Width / (double)this.Image.Width)); //自分のTop = [親(Panel)の高さの半分] - [自分の高さの半分]    ←上下中央寄せにしたい為 this.Top = (int)((Parent.Height / 2.0) - (this.Height / 2.0)); } } else { //↑のif文と逆の計算 this.Width = (int)(this.Parent.Width / 2.0); this.Height = (int)(Image.Height * ((double)this.Width / (double)this.Image.Width)); this.Top = (int)((Parent.Height / 2.0) - (this.Height / 2.0)); if (this.Height > (this.Parent.Height)) { this.Height = (int)(this.Parent.Height); this.Width = (int)(Image.Width * ((double)this.Height / (double)this.Image.Height)); this.Top = 0; } } if (this.side == eSide.Right) { //右ページなら画像の左端は、親(Panel)の中央 this.Left = (int)(this.Parent.Width / 2.0); } else { //左ページなら画像の左端は、親(Panel)の中央 - 画像の幅  ←  本のように中央に寄せたい為 this.Left = (int)((this.Parent.Width / 2.0) - this.Width); } }

  • pictureboxの画像を削除するには?

    Visual C# 2008を使用しています。 ボタンを押すとpictureboxに画像が表示されるプログラムを作成しました。 今度は、その表示された画像をクリックし、別のボタンを押すことでその画像が削除できるようにしたいと思っているのですが、うまくプログラムできません。 誰か教えていただけないでしょうか? ソースコードは以下のようになっています。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace algorithm { public partial class Form1 : Form { int space = 10; public Form1() { InitializeComponent(); } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { } private void kihonsyori_Click(object sender, EventArgs e) { if (this.pictureBox1.Image == null) { Bitmap img = new Bitmap(@"C:\Documents and Settings\admin\My Documents\Visual Studio 2008\Projects\algorithm\algorithm\picture\基本処理箱.png"); Graphics g = pictureBox1.CreateGraphics(); g.DrawImage(img, 0, space, 0.4F * img.Width, 0.3F * img.Height); space += 50; img.Dispose(); g.Dispose(); } else { this.pictureBox1.Image.Dispose(); this.pictureBox1.Image = null; } } private void groupBox1_Enter(object sender, EventArgs e) { } private void pictureBox1_Click(object sender, EventArgs e) { } private void hanpuku_Click(object sender, EventArgs e) { if (this.pictureBox1.Image == null) { Bitmap img = new Bitmap(@"C:\Documents and Settings\admin\My Documents\Visual Studio 2008\Projects\algorithm\algorithm\picture\反復箱.png"); Graphics g = pictureBox1.CreateGraphics(); g.DrawImage(img, 0, space, 0.4F * img.Width, 0.3F * img.Height); space += 80; img.Dispose(); g.Dispose(); } else { this.pictureBox1.Image.Dispose(); this.pictureBox1.Image = null; } } private void sentaku_Click(object sender, EventArgs e) { if (this.pictureBox1.Image == null) { Bitmap img = new Bitmap(@"C:\Documents and Settings\admin\My Documents\Visual Studio 2008\Projects\algorithm\algorithm\picture\選択箱.png"); Graphics g = pictureBox1.CreateGraphics(); g.DrawImage(img, 0, space, 0.4F * img.Width, 0.3F * img.Height); space += 80; img.Dispose(); g.Dispose(); } else { this.pictureBox1.Image.Dispose(); this.pictureBox1.Image = null; } } private void sakujyo_Click(object sender, EventArgs e) { pictureBox1.Image = null; space = 0; } } }

  • 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); よろしくおねがいいたします。

  • (VB.net)画像の一部を切り取って,拡大・縮小したい

    内容を見ていただき,ありがとうございます。 タイトルのままですが,PictureBoxに画像を出して,その一部を切り取ったうえで拡大・縮小させるプログラムを作成しています。 今のところ,画像の一部を切り出すことはできたのですが,これを拡大・縮小させる方法をご存じないでしょうか。画像の一部を切り出すソースは以下のとおりです。 まず,フォームにピクチャボックス(PictureBox1)とコマンドボタン(Button1)を配置し,ソリューションを置いているフォルダ内のbinフォルダにWinter.jpg(WinXPのマイピクチャに入っているものです)を入れておきます。次に,Button1のコードとして,以下のものを打ち込みました。 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Bitmapオブジェクトの作成(切り取り元画像の指定) Dim bmap = New Bitmap("winter.jpg") '切り取る範囲をRectanleで指定 Dim rect As New Rectangle(10, 20, 100, 200) 'PictureBox1のGraphicsオブジェクトの作成 Dim g As Graphics = PictureBox1.CreateGraphics() '元の画像の指定部分を切り取ってPictureboxの左上隅(x,y = 0,0)に表示する.単位はピクセル. g.DrawImage(bmap, 0, 0, rect, GraphicsUnit.Pixel) 'Graphicsオブジェクトを破棄 g.Dispose() End Sub 以上です。 おそらく,DrawImageの部分を変えればいけるのではないかと思われるのですが,いかがでしょうか。ご助言よろしくお願いいたします。

  • C#のGraphicsクラスについて(GDI+)

    以下のようにgraphicsクラスをつかった画像の描画をおこないました。 Graphics gr = Graphics.FromImage(mapObj); というふうにからのリソースから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.ComponentModel; public class MainClass{ public static void Main(string [] args){ NewForm formObj = new NewForm(); formObj.RenderMethod(); Application .Run(formObj); } } public class NewForm : Form{ public NewForm(){ this.Width = 500; this.Height = 500; } public void RenderMethod(){ Bitmap mapObj = new Bitmap(500,500); Graphics gr = Graphics.FromImage(mapObj); Image imageObj = Image.FromFile("C:\\test.jpg"); gr .DrawImage(imageObj, 0,0,150,150); this.BackgroundImage = mapObj; } } このほかに、フォームコントロールの thisl.CreateGraphics()という メソッドを使っても画像を描画できるとききました。 あるサンプルをみると public class NewForm : Form{ public NewForm(){ this.Width = 500; this.Height = 500; } public void RenderMethod(){ Graphics gr = this.CreateGraphics(); Image imageObj = Image.FromFile("C:\\test.jpg"); gr .DrawImage(imageObj, 0,0,150,150); } } とこのようにthis.CreateGraphics()をつかっていましたが 実際にはこれが描画されないのです。 Graphics gr = Graphics.FromImage(mapObj); というGraphicsクラスの静的メソッドを使う方法ではなく コントロールのCreateGraphicsメソッドをつかって描画するにはどうしたらよいのですか? 識者のかた、ご教授ください。

  • IplImageのBmp変換。

    いつもこちらでお世話になっております。 現在OpenCVをつかって画像処理を行なっています。言語はVC++です。 OpenCVで処理した画像をPictureBoxに表示させたいのですが、 うまく表示できません。 IplImageをBmpに変換させる部分がおかしいとは思うのですが… //-------------------------------------------------------- IplImage *img; img = cvLoadImage(filename, 0); // 0: グレイスケールで読み込む cvThreshold(img,img, 70, 255, CV_THRESH_BINARY_INV);//二値化 // ビットマップ用のカラーバッファ char* ColorBuf = (char*)calloc( sizeof(char), img->width * img->height * 4 ); for( int y = 0; y < img->height; y++ ) { for( int x = 0; x < img->width; x++ ) { // Blue ColorBuf[ y * img->width + x * 4 + 0 ] = img->imageData[ y * img->widthStep + x * 3 + 0 ]; // Green ColorBuf[ y * img->width + x * 4 + 1 ] = img->imageData[ y * img->widthStep + x * 3 + 1 ]; // Red ColorBuf[ y * img->width + x * 4 + 2 ] = img->imageData[ y * img->widthStep + x * 3 + 2 ]; } } mbmp.CreateBitmap( img->width, img->height, 1, 32, ColorBuf ); free( ColorBuf ); ((CStatic*)GetDlgItem( IDC_IMGSRC ))->SetBitmap ( mbmp ); myDC.CreateCompatibleDC(pDC); CBitmap *oldBMP = myDC.SelectObject(&mbmp); pDC->BitBlt(0,0,300,300,&myDC,0,0,SRCCOPY); myDC.SelectObject(oldBMP); cvReleaseImage( &img ); //----------------------------------------------- PictureBoxのリソースIDをIDC_IMGSRCに設定しています。 今はPictureBoxの上部に画像が4つ並んでしまっている状態です。 なにか改善すべきところがありましたらよろしくお願いいたします。

  • C# PictureBoxへの描画を行うとメッセージボックスが表示されません。

    いつもお世話になっております。 ふと、PictureBoxに描画をしてみようかと思い Paintイベントに以下のようにして描画を行いました。   myBitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);   Graphics g = Graphics.FromImage(myBitmap);   pictureBox1.Image = myBitmap;      ・      ・(g.FillRectangle(・・・);など)      ・ (myBitmapはBitmap型のグローバル変数です。) PictureBoxは、自由にサイズ変更可能なフォームに貼り付け、四方をanchorで固めています。 そのためサイズが変更されるたびに、描画するときBitmapのサイズも変更しないといけないので この処理を毎回通っています。 (その部分が何だか無駄な気がするのですが、他にいい方法が思いつきませんでした。) ところが、描画はうまくいったのですが、メッセージボックスを出すと そのメッセージボックスが一度Altキーを押さないと表示されません。 色々調べて、ダブルバッファリングというものがあると知りました。 そこでPaintイベントで最初にこの処理をし   Image image = new Bitmap(pictureBox1.Width, pictureBox1.Height);   g = Graphics.FromImage(image); gに描画し終わってから   e.Graphics.DrawImage(image, 0, 0); としてみると、今度はフォームを移動するのにも一瞬フリーズするようになってしまいました。 (あまり深くプログラミングをしたことがないので、理解しきれなくてやり方が悪いのかも知れません・・・) メッセージボックスが表示されないのは、PictureBoxの描画のため 他のコントロールの描画ができていなためだと推測しているのですが どう直していいかわかりません。 どなたか、わかる方がいましたらよろしくお願いします。 <補足> OS:Vista VisualStudio2008 .NET Framework3.5