• 締切済み

BMP画像を画像処理して連続に表示したいですが?

プログラミングの初心者ですが、現在VC++2005のフォームアプリケーションでプログラムについて勉強しています。画像を連続表示するところで、わからなくなってしまったので、みなさんのご指導お願いいたします。 やりたい処理は、取り込んだ画像の色を変化させて、順番に表示したい 処理です。作成したプログラムは下記のようになります。 前略 for(n=0; n<256; n+=20){ for(y=0; y<480; y++){ for(x=0; y<640; x++){ bmp->Setpixel(x, y, Color::FromArgb(n, n, n)); } } pictureBox->Image = bmp; Thread::Sleep(2000); } nの値をbmpに入れてから一回表示し、さらにnを足してからbmpに入れて表示するといった流れですが、Sleepを入れても何にも表示されません。 どういったところは不具合なのかをよくわかりません。 ご指導をいただければ感謝致します。どうぞよろしくお願い致します。

みんなの回答

  • prophetok
  • ベストアンサー率44% (13/29)
回答No.4

#3さんへ 別に君が書いたサンプルコードに対してコメントしたわけではありませんよ。 あなたもよく理解できていないようですが、質問者のコードでは、描画できません。 表に出てこないだけですが、Invalidate()をコールするとWM_PAINTのメッセージがアプリケーションに対して送られて描画される仕組みは、同じですよ。

回答No.3

 こんにちは。チョッと不足したので補足を。  現状では真っ黒なビットマップをSetPixel()で着色しているだけなので、黒→白になるだけですので、ファイルから開いたビットマップの場合は、元の色彩を取って計算しないといけません。  後、xボタンを押した時、スレッドが存在していれば止めるためにClosingイベントを追加しています。その他の細かい事は任意でお願いします。 ・このコードでは、アプリケーションがWM_PAINTを処理できないので描画できません。  描画出来ています。  ピクチャボックスのImageプロパティにグラフィックをセットしてInvalidate()を呼ぶとピクチャボックスの表示が更新されます。    通常、C++/CLIにWM_PAINTなんてものは出て来ません。  WndProcメソッドをオーバーライドして、WM_PAINTを引っ掛ける事は出来る様に成っていますが、余程の理由でもない限り、行いません。  処理する場合、PlatformSDK側のAPIや構造体を使用する為にwindows.hをインクルードしなければいけない必要性が発生する事がありますが、C++/CLIでは通常使用出来ない様に成っています。    ピクチャボックスにグラフィックスをセットして反映させるだけなら、Paintイベントをオーバーライドする必要はありません。   ・まず、Windowsにおける描画の仕組みを理解しましょう。  揚げ足を取る前に、マネージドとアンマネージドの違い位は見抜きましょう。 #pragma once namespace laylay { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// <summary> /// Form1 の概要 /// /// 警告: このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた /// マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを /// 変更する必要があります。この変更を行わないと、 /// デザイナと、このフォームに関連付けられたローカライズ済みリソースとが、 /// 正しく相互に利用できなくなります。 /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: ここにコンストラクタ コードを追加します // } protected: /// <summary> /// 使用中のリソースをすべてクリーンアップします。 /// </summary> ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button1; protected: private: System::Windows::Forms::PictureBox^ pictureBox1; private: /// <summary> /// 必要なデザイナ変数です。 /// </summary> System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// <summary> /// デザイナ サポートに必要なメソッドです。このメソッドの内容を /// コード エディタで変更しないでください。 /// </summary> void InitializeComponent(void) { this->button1 = (gcnew System::Windows::Forms::Button()); this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox()); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->BeginInit(); this->SuspendLayout(); // // button1 // this->button1->Location = System::Drawing::Point(64, 238); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(75, 23); this->button1->TabIndex = 0; this->button1->Text = L"button1"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); // // pictureBox1 // this->pictureBox1->Location = System::Drawing::Point(13, 13); this->pictureBox1->Name = L"pictureBox1"; this->pictureBox1->Size = System::Drawing::Size(257, 179); this->pictureBox1->TabIndex = 1; this->pictureBox1->TabStop = false; // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 12); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(292, 273); this->Controls->Add(this->pictureBox1); this->Controls->Add(this->button1); this->Name = L"Form1"; this->Text = L"Form1"; this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->EndInit(); this->ResumeLayout(false); } #pragma endregion //メンバ変数 private: delegate System::Void DelegateSetPixel(System::Int32 n); private: DelegateSetPixel^ setPixel; private: System::Threading::Thread^ thread; private: System::Drawing::Bitmap^ bitmap; //下ごしらえ private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { //ファイルから読む System::Drawing::Bitmap^ bmpFromFile = dynamic_cast<System::Drawing::Bitmap^>(gcnew System::Drawing::Bitmap("test.bmp")); this->bitmap = dynamic_cast<System::Drawing::Bitmap^>(bmpFromFile->Clone()); delete bmpFromFile; this->pictureBox1->Image = dynamic_cast<System::Drawing::Bitmap^>(this->bitmap->Clone()); this->setPixel = gcnew DelegateSetPixel(this, &Form1::DrawMethod); this->button1->Tag = gcnew System::Boolean(false); this->Closing += gcnew System::ComponentModel::CancelEventHandler(this, &Form1::Form1_Closing); } //閉じた時 private: System::Void Form1_Closing(System::Object^ sender, System::ComponentModel::CancelEventArgs^ e) { if(this->thread != nullptr) this->thread->Abort(); } //スレッド中 private: System::Void threadProc() { System::Int32 n = 0; while(*static_cast<System::Boolean^>(this->button1->Tag)) { this->thread->Sleep(33); n = System::Math::Min(n + 20, 255); this->setPixel->Invoke(n); if(n == 255)n = 0; } } //ボタンを押す private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { System::Boolean% rbRunning = *static_cast<System::Boolean^>(this->button1->Tag); rbRunning ^= true; if(rbRunning) { this->button1->Text = L"実行中"; this->thread = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(this, &Form1::threadProc)); this->thread->Start(); } else { this->button1->Text = L"レディ"; this->thread->Abort(); this->thread = nullptr; } this->button1->Update(); } private: System::Void DrawMethod(System::Int32 n) { System::Drawing::Bitmap^ bmp = dynamic_cast<System::Drawing::Bitmap^>(this->bitmap->Clone()); for(System::Int32 y = 0; y < bmp->Height; y++) { for(System::Int32 x = 0; x < bmp->Width; x++) { Color color = bmp->GetPixel(x, y); System::Int32 R = System::Math::Min(color.R + n, 255); System::Int32 G = System::Math::Min(color.G + n, 255); System::Int32 B = System::Math::Min(color.B + n, 255); bmp->SetPixel(x, y, Color::FromArgb(R, G, B)); } } System::Drawing::Image^ image = this->pictureBox1->Image; this->pictureBox1->Image = bmp; delete image; this->pictureBox1->Invalidate(); } }; }

  • prophetok
  • ベストアンサー率44% (13/29)
回答No.2

このコードでは、アプリケーションがWM_PAINTを処理できないので描画できません。 まず、Windowsにおける描画の仕組みを理解しましょう。

回答No.1

 こんにちは。  Thread::Sleep(2000);の2000が大きすぎて、止まっているのではないでしょうか。33位にしてみては如何でしょうか。  後、SetPixel()は負担の大きいメソッドですので、640x480のイメージを処理するのは実に厳しいと思います。  以下は320x240のイメージ変色をスレッドにて行っていますが、かなり重たいです。pictureBox1とbutton1が必要です。参考程度に。 //メンバ変数 private: delegate System::Void DelegateSetPixel(System::Int32 n); private: DelegateSetPixel^ setPixel; private: System::Threading::Thread^ thread; //下ごしらえ private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { //ファイルから読むなら //System::Drawing::Bitmap^ bmpFromFile = dynamic_cast<System::Drawing::Bitmap^>(gcnew System::Drawing::Bitmap("test.bmp")); //this->pictureBox1->Image = dynamic_cast<System::Drawing::Bitmap^>(bmpFromFile->Clone()); //delete bmpFromFile; this->setPixel = gcnew DelegateSetPixel(this, &Form1::DrawMethod); this->pictureBox1->Image = gcnew System::Drawing::Bitmap(320, 240, System::Drawing::Imaging::PixelFormat::Format24bppRgb); this->button1->Tag = gcnew System::Boolean(false); } //スレッド中 private: System::Void threadProc() { System::Int32 n = 0; while(*static_cast<System::Boolean^>(this->button1->Tag)) { this->thread->Sleep(33); n = System::Math::Min(n + 20, 255); this->setPixel->Invoke(n); if(n == 255)n = 0; } } //ボタンを押す private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { System::Boolean% rbRunning = *static_cast<System::Boolean^>(this->button1->Tag); rbRunning ^= true; if(rbRunning) { this->button1->Text = L"実行中"; this->thread = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(this, &Form1::threadProc)); this->thread->Start(); } else { this->button1->Text = L"レディ"; } this->button1->Update(); } //色彩変化 private: System::Void DrawMethod(System::Int32 n) { //ピクチャボックスからイメージを複製 System::Drawing::Bitmap^ bitmap = dynamic_cast<System::Drawing::Bitmap^>(this->pictureBox1->Image->Clone()); //色彩変化 for(System::Int32 y = 0; y < bitmap->Height; y++) { for(System::Int32 x = 0; x < bitmap->Width; x++) { bitmap->SetPixel(x, y, Color::FromArgb(n, n, n)); } } //古いイメージと交換する System::Drawing::Image^ image = this->pictureBox1->Image; this->pictureBox1->Image = bitmap; delete image; this->pictureBox1->Invalidate(); }

関連するQ&A

  • VB2005での画像処理

    各ピクセルの色情報を取得し,そのままコピーするプログラムです. 同じ大きさのPictureBox1とPictureBox2,それとButton1があります. エラーや警告は出ませんが,Button1をクリックしてもまったく動きません. 最終的にはPictureBox1を画像処理してPictureBox2に結果を表示させたいのですが,そのままコピーも出来ないため原因を探しています. A = picture1.GetPixel(i, j).ToArgb() picture2.SetPixel(i, j, color.FromArgb(A)) ではだめなのでしょうか.ご教授ください. -----以下プログラム------ Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i As Integer Dim j As Integer Dim A As Integer Dim picture1 As New Bitmap(PictureBox1.Image) Dim picture2 As New Bitmap(PictureBox2.Image) For j = 0 To PictureBox1.Width - 1 For i = 0 To PictureBox1.Height - 1 A = picture1.GetPixel(i, j).ToArgb() picture2.SetPixel(i, j, color.FromArgb(A)) Next Next End Sub

  • 複数枚画像の合成

    こんにちわ、Visual Studio 2005のフォームアプリケーションでプログラミングしているものです。複数枚の画像を一つの画像にしたいのですが、どうもあっているのかどうかわかりません。 たとえば、画像が9枚あって1枚目の一番左上の画素を[0-0,0]とすると、 [0-0,0][1-0,0][2-0,0]|[3-1,0][4-1,0][5-1,0]|[6-2,0][7-2,0][8-2,0] [0-0,0][1-0,0][2-0,0]|[3-1,0][4-1,0][5-1,0]|[6-2,0][7-2,0][8-2,0] [3-0,0][4-0,0][5-0,0]|[6-1,0][7-1,0][8-1,0]|[0-2,0][1-2,0][2-2,0] [3-0,0][4-0,0][5-0,0]|[6-1,0][7-1,0][8-1,0]|[0-2,0][1-2,0][2-2,0] [6-0,0][7-0,0][8-0,0]|[0-1,0][1-1,0][2-1,0]|[3-2,0][4-2,0][5-2,0] [6-0,0][7-0,0][8-0,0]|[0-1,0][1-1,0][2-1,0]|[3-2,0][4-2,0][5-2,0] -----------------+-----------------+------------------------ [0-0,1][1-0,1][2-0,1]|[3-1,1][4-1,1][5-1,1]|[6-2,1][7-2,1][8-2,1] [0-0,1][1-0,1][2-0,1]|[3-1,1][4-1,1][5-1,1]|[6-2,1][7-2,1][8-2,1] [3-0,1][4-0,1][5-0,1]|[6-1,1][7-1,1][8-1,1]|[0-2,1][1-2,1][2-2,1] [3-0,1][4-0,1][5-0,1]|[6-1,1][7-1,1][8-1,1]|[0-2,1][1-2,1][2-2,1] [6-0,1][7-0,1][8-0,1]|[0-1,1][1-1,1][2-1,1]|[3-2,1][4-2,1][5-2,1] [6-0,1][7-0,1][8-0,1]|[0-1,1][1-1,1][2-1,1]|[3-2,1][4-2,1][5-2,1] -----------------+-----------------+------------------------ [0-0,2][1-0,2][2-0,2]|[3-1,2][4-1,2][5-1,2]|[6-2,2][7-2,2][8-2,2] [0-0,2][1-0,2][2-0,2]|[3-1,2][4-1,2][5-1,2]|[6-2,2][7-2,2][8-2,2] [3-0,2][4-0,2][5-0,2]|[6-1,2][7-1,2][8-1,2]|[0-2,2][1-2,2][2-2,2] [3-0,2][4-0,2][5-0,2]|[6-1,2][7-1,2][8-1,2]|[0-2,2][1-2,2][2-2,2] [6-0,2][7-0,2][8-0,2]|[0-1,2][1-1,2][2-1,2]|[3-2,2][4-2,2][5-2,2] [6-0,2][7-0,2][8-0,2]|[0-1,2][1-1,2][2-1,2]|[3-2,2][4-2,2][5-2,2] といった感じにしようと思っています。 画像には配列を使っていてbmp[0]~[8]が9枚の画像を表し、picはbmp[0]~[8]の画像一枚に対してサイズが横3倍縦6倍の画像になるはずです。 自分でプログラムしたのですが、できた画像を拡大しても細かすぎてよくわからない状況です。 for ( y = 0; y < h; y += 6 ) {  for ( x = 0; x < w; x += 3 ) {   for( b = 0; b < 3; b++ ) {    for( a = 0; a < 3; a++ ) {      pic->SetPixel( x + a, y + (2 * b) , bmp[((2 * b) * 3 + a + x) % 9]->GetPixel( x / 3, y / 6 ) );      pic->SetPixel( x + a, y + (2 * b) , bmp[(((2 * b) + 1) * 3 + a + x) % 9]->GetPixel( x / 3, y / 6 ) );    }   }  } } 以前に、同じような配置法なのですが横3倍縦3倍の合成画像として、 [0-0,0][1-0,0][2-0,0]|[3-1,0][4-1,0][5-1,0]|[6-2,0][7-2,0][8-2,0] [3-0,0][4-0,0][5-0,0]|[6-1,0][7-1,0][8-1,0]|[0-2,0][1-2,0][2-2,0] [6-0,0][7-0,0][8-0,0]|[0-1,0][1-1,0][2-1,0]|[3-2,0][4-2,0][5-2,0] -----------------+-----------------+------------------------ [0-0,1][1-0,1][2-0,1]|[3-1,1][4-1,1][5-1,1]|[6-2,1][7-2,1][8-2,1] [3-0,1][4-0,1][5-0,1]|[6-1,1][7-1,1][8-1,1]|[0-2,1][1-2,1][2-2,1] [6-0,1][7-0,1][8-0,1]|[0-1,1][1-1,1][2-1,1]|[3-2,1][4-2,1][5-2,1] -----------------+-----------------+------------------------ [0-0,2][1-0,2][2-0,2]|[3-1,2][4-1,2][5-1,2]|[6-2,2][7-2,2][8-2,2] [3-0,2][4-0,2][5-0,2]|[6-1,2][7-1,2][8-1,2]|[0-2,2][1-2,2][2-2,2] [6-0,2][7-0,2][8-0,2]|[0-1,2][1-1,2][2-1,2]|[3-2,2][4-2,2][5-2,2] の配置プログラムとして、 for ( y = 0; y < h; y += 3 ) {  for ( x = 0; x < w; x += 3 ) {   for( b = 0; b < 3; b++ ) {    for( a = 0; a < 3; a++ ) {      pic->SetPixel( x + a, y + b , bmp[(b * 3 + a + x) % 9]->GetPixel( x / 3, y / 3 ) );    }   }  } } との回答をいただいたので、これを参考に作ってみたのですが・・・。 説明が下手でわかりにくいかもしれませんが、お分かりの方がいましたらご教授お願いします。

  • Octave画像ファイルの向きを上下逆さまにしたい

    % lec5_2.m % bmp画像出力(画像*****のプログラム) % % Input.bmp : 処理したい画像(モノクロ)BMPファイル % Output.bmp : 処理したい画像(モノクロ)BMPファイル clear all, close all % おまじない Iin=imread('LENNA.bmp'); % 画像の入力 figure, imshow(Iin) % 画像表示 [y_size,x_size]=size(Iin); %縦横のサイズ % 配布資料を参考にして以下の2重for文の % 中のプログラムを完成させよ for n=3:y_size for m=3:x_size n2=y_size-n+1; Iout(n,m)=Iin(n2,m); end end % プログラム修正ここまで figure, imshow(Iout) % 画像表示 bmpwrite_bk(Iout,'LENNA.bmp','Output.bmp'); % End of file 上記はOctaveのmファイルの中身ですが、これを使用すると画像の上下を反転させることができます。 これを、『画像を上下逆さまに回転』させ、元の画像を下向きにして表示させたいです。それをするには二重for文の内容を変えるみたいなのですが、どう変えればいいかわからず途方に暮れています。 二重for文の内容をどう変えたら成功するのでしょうか。 mファイルをどう弄ったらいいかもわからない初心者の質問です。よろしくお願いします。

  • C#について

    C#で画像の色を変更しようと頑張っているのですが・・・ クリックした座標と同じ色の場所を青にしたいです。 XとYは画像の座標の大体の最大値です。 public partial class Form1 : Form { Bitmap bmp; public Form1() { InitializeComponent(); bmp = new Bitmap(pB1.Image); } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { Color c = bmp.GetPixel(e.X, e.Y); for (int Y = 0; Y < 280; Y++) { for (int X = 0; X < 240; X++) { Color c2 = bmp.GetPixel(X, Y); if (c.R == c2.R && c.G == c2.G && c.B == c2.B) { bmp.SetPixel(X, Y, Color.FromArgb(0, 0, 255)); } } } } } } クリックしたところと同じ色を青(0, 0, 255)に変更したいのですが変更されません。 間違っているところなどがあれば教えてください。

  • C言語でbmp モノクロ画像の画像処理を行い、x・y・1or0 をtx

    C言語でbmp モノクロ画像の画像処理を行い、x・y・1or0 をtxtファイルに出力するプログラムを作らないといけないのですが、下記のプログラムのどこをいじればいいかわかりませんだれかお分かりになりませんか? #include <stdio.h> #include <process.h> #define COEF 0.1; #define X_CHORD_Max 512 #define Y_CHORD_Max 512 unsigned long *readBmp(char *filename); main() { unsigned long *pBmp; char input[64], output[64]; int x, y, k,n; char temp; float xCodnt,yCodnt; unsigned long hight, wight; unsigned int maskBit = 0x0080; /* Record the data*/ printf("Input file.bmp :"); scanf("%s", input); printf("output file.txt :"); scanf("%s", output); FILE *fpTxt; if((fpTxt=fopen(output,"wt"))==NULL) { printf("Cannot open file strike any key exit!"); getch(); exit(1); } pBmp = readBmp(input); /* Read the Data*/ hight = *(pBmp+1); wight = *pBmp/32; pBmp += 2; for (y=0;y<hight; y++) { for (x=0; x<wight; x++) { for (k=0; k<4; k++) { temp =(char)*pBmp; for (n=0; n<8; n++) { if(temp & maskBit) { xCodnt = COEF; yCodnt =COEF; xCodnt = xCodnt*(x*32+k*8+n); yCodnt = yCodnt*y; fprintf(fpTxt,"G01 X%f Y%f\n",xCodnt,yCodnt); } maskBit >>= 1; } *pBmp >>= 8; maskBit = 0x0080; } pBmp ++; } } rewind(fpTxt); fclose(fpTxt); //END

  • 画像表示についてです

    picturebox1とpicturebox2を使ってbmpとjpgの画像を表示させています。 そこで、画像にも色々サイズがあるのですがpictureboxのwidthのサイズに合わせて画像の縮小拡大したいのですがどうやっていいのかわかりません。 教えてくださいお願いしますm(__)m ちなみにHeightはスクロールバーを使ってます。 Widthはスクロールバーを使わないプログラムです。

  • 線形補間法プログラム(C++)

    C++言語で線形補間法のプログラムを組んで実行しているのですが、どしてもうまくいきません。ただ2倍の画像を作っているだけなのですが・・・。 以下プログラムを載せます、おおよその場所はわかるのですがどうすれば通るのかわかりません。どう直したらよいのか分かる方がいましたらご教授お願いします。 ※bmp[0]:現画像 pic:bmp[0]の縦横2倍の画像 // 線形補間法 // int zx = 2; int zy = 2; int i,j,m,n; float x,y,p,q; int xs = bmp[0]->Width/2; int ys = bmp[0]->Height/2; int d; for(i = -ys; i < ys; i++){ for(j = -xs; j < xs; j++){ y = i/zy; x = j/zx; if(y > 0){ m = (int)y; }else{ m = (int)(y-1);} if(x > 0){ n = (int)x; }else{ n = (int)(x-1);} q = y - m; p = x - n; if(q == 1){q = 0; m = m + 1;} if(p == 1){p = 0; n = n + 1;} if((m >= -ys)&&(m < ys)&&(n >= -xs)&&(n < xs)){ d = (int)((1.0 - q) * (1.0 - p) * (bmp[0]->GetPixel( m + ys, n + xs)) //おそらくこの辺に問題があるかと思われます。 + p * (bmp[0]->GetPixel( m +ys, n + xs)) + q * (1.0 - p) * (bmp[0]->GetPixel(m + 1 + ys, n + xs)) + p * (bmp[0]->GetPixel(m + 1 + ys, n + 1 + xs))); }else{ d = 0; } if(d < 0){d = 0;} if(d > 255){d = 255;} pic->SetPixel(i + ys, j + xs) = d; } } pictureBox2->Image = pic; }

  • C#でピクチャーボックスに文字を描画、出力する方法

    以下のことをやりたいのですが、できなくて困っています。 Microsoft Visual Studio 2010 Expressを使っています。 1、ピクチャーボックスに色画像を表示 2、ピクチャーボックスに文字(説明)も表示 3、その画像を出力 2まではできたのですが、2までできたあとに、プログラムのウインドウ上に他のウインドウ(マイコンピュータやマイドキュメントなど)が表示されたり、プログラムのウインドウを最小化したりして、一度隠れてしまうと文字が消えてしまうようで、画像のみの出力になってしまいました。(画像のみの出力には成功しています) なんとかして出力する方法はないでしょうか。 以下がソースです。よろしくお願いします。 ボタン1 Bitmap bmp6 = new Bitmap(800, 600); int sr, sg, sb, i, j; sr = 100; sg = 100; sb = 100; for (i = 0; i < 40; i++) { for (j = 0; j < 40; j++) { bmp6.SetPixel(i, j, Color.FromArgb(sr, sg, sb)); } pictureBox1.Image = bmp6; } ボタン2 string s1 = "文字"; Font newfont = new Font("MS明朝", 10); pictureBox1.CreateGraphics().DrawString(s1, newfont, Brushes.Blue, 50, 200); ボタン3 pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, 800, 600)); bmp.Save(c:\\a.png);

  • 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つ並んでしまっている状態です。 なにか改善すべきところがありましたらよろしくお願いいたします。

  • Bitmapデータ型の画像幅の拡大

    現在Visual studio 2005のフォームアプリケーションでプログラミグを行っている者です。以下のプログラムの中に画像の幅であるw,hという変数があるのですが、私の作ろうとしている画像処理の関係上、この画像データの幅を倍にしたいです(例:3*w,3*h)。しかし、変数宣言(例:int 3*w)やfor文の中で倍にしようとしても、ビルドはできるものの"アプリケーションのコンポーネントで、ハンドルされていない例外が発生しました。・・・パラメータは正の値で、高さより小さい値指定しなければなりません。"とでて、実行できません。おそらく倍にしてあげたとこで、倍になった部分の画像データがわからないためこういったエラーが出てしまうのだと考えています。どうにかして、BITMAPデータ型で読み込んだ画像の幅を倍の数値を得たいのですが、エラーのでないようにするためにはどのようにしてあげればいいのでしょうか?わかる方がいたらよろしくお願いします。 プログラムは以下のとおりです。 #pragma once // 省略 // } #pragma endregion private:Bitmap^ pic; private:array< Bitmap^>^ bmp; // 原画像格納 // private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { bmp[0] = gcnew Bitmap("画像ファイル1",true); pictureBox1->Image = bmp[0]; pic = gcnew Bitmap("画像ファイル2",true); } private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { int x,y,a,b; /* x,y: 画像の座標 a,b: 複合画像の座標 */ int w = pic->Width; //ここでpic->Width*3としてもエラー /* 複合画像の横幅 */ int h = pic->Height; /* 複合画像の縦幅 */ x = 0; y = 0; a = 0; b = 0; for(y = 0; y < h; y++){ //ここで3*hとしてもエラー for(x = 0; x < w; x+=3){ pic->SetPixel( x, y, bmp[1]->GetPixel( x, y ) ); } } pictureBox2->Image = pic; } }; }