質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

解決済

1回答

6570閲覧

Virtual Sutudio 2017 でペイントソフトを作る事から始めましたが…

mistletoe

総合スコア7

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

0クリップ

投稿2018/05/22 18:13

編集2018/05/22 18:17

前提・実現したいこと

現在 Virtual Sutudio 2017 で言語の勉強の先駆けにペイントソフトを作っています。
ネットに落ちているサンプルコードを切り貼りしたり打ち込みながら自分なりの解釈で作ろうと実践していました。自分用のノートもかねてなので汚いメモと考えてもらえればよいですが、何もかも分からないスタートです故、お手柔らかにお願いします。
まず、見つかったYoutubeの動画を見ながらpictureBoxに線を引いたり、キャンバスカラーを変更することに成功しましたが、少しずつ変えながら何度も失敗し、これだけで20回はソリューションを作り直しました。
参考1:https://www.youtube.com/watch?v=gFIVnfw1Ywo&list=PLzEbEpZ4njvPYeBohSQ9te-W_L27r77Fg&index=80
参考2:https://www.codeproject.com/Tips/811495/Simple-Paint-Application-in-Csharp
参考3:http://skylinker.blog.fc2.com/blog-entry-57.html

発生している問題・エラーメッセージ

コードがややこしくなった為かpictureBox1に線が引けなくなる問題。
コードがややこしくなった為かキャンバスカラーを変えられていたのに、ボタンが反応しなくなる問題。
このデータはもう重いので作り直すか迷っていますが、分からないので疲れてしまいました。

該当のソースコード

C#

1using System; 2using System.Drawing; 3using System.Drawing.Imaging; 4using System.Windows.Forms; 5 6namespace WindowsFormsApp4 7{ 8 public partial class Form1 : Form 9 { 10 public Form1() 11 { 12 InitializeComponent(); 13 g = pB1.CreateGraphics(); 14 } 15 Graphics g; 16 17 private void Form1_Load(object sender, EventArgs e) 18 { 19 bmp = new Bitmap(pB1.Width, pB1.Height); 20 draw(); 21 } 22 private static void draw() 23 { 24 } 25 26 Bitmap bmp = null; 27 bool drawFlg = false; //true:描画中 28 29 //nullable int for staing Null value 30 int? initX = null; 31 int? initY = null; 32 bool startPaint = false; 33 bool drawSquare = false; 34 bool drawRectangle = false; 35 bool drawCircle = false; 36 37 //Event Fired when mouse pointer is over Panel and a mouse button is pressed 38 private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 39 { 40 //描画中にする 41 drawFlg = true; 42 43 pictureBox1_MouseMove(sender, e); 44 /* 45 startPaint = true; 46 if (drawSquare) 47 { 48 //Use Solid Brush for filling the graphic shaoes 49 SolidBrush sb = new SolidBrush(btnPenColor.BackColor); 50 //Setting the width and height some for creating square. 51 //Getting the width and Height value from Texitbox(txtShapeSize) 52 g.FillRectangle(sb, e.X, e.Y, int.Parse(txtShapeSize.Text), int,Parse(txtShapeSize.text)); 53 //Setting startPaint and drawSquere value to false for creating one graphic on one click. 54 startPaint = false; 55 drawSquare = false; 56 } 57 if (drawRectangle) 58 { 59 SolidBrush sb = new SolidBrush(btnPenColor, BackColor); 60 g.FillRectangle(sb, e.X, e.Y, 2*int.Parse(txtShapeSize.Text)); 61 startPaint = false; 62 drawRectangle = false; 63 } 64 if (drawCircle) 65 { 66 SolidBrush sb = new SolidBrush(btnPenColor.BackColor); 67 g.FillEllipse(sb, e.X, e.Y, int.Parse(txtShapeSize.Text)); 68 startPaint = false; 69 drawCircle = false; 70 } 71 */ 72 } 73 74 Point oldLocation = new Point(); 75 76 //Event fired when the mouse pointer is moved over the Panel(pictureBox). 77 private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 78 { 79 80 //test 81 using (Graphics g = Graphics.FromImage(bmp)) ; 82 { 83 g.DrawLine(Pens.Blue, oldLocation, e.Location); 84 //新しい位置を保存する 85 oldLocation = e.Location; 86 } 87 pB1.Image = bmp; 88 89 //描画中でない場合は、処理を抜ける 90 if (drawFlg == false) return; 91 { 92 //描画する 93 //draw(g.oldLocation, e.Location); 94 } 95 96 if(startPaint) 97 { 98 /* 99 //Setting the Pen BackColor and Line whdth 100 Pen p = new Pen(btnPenColor.BackColor, float.Parse(cmbPenSize.Text)); 101 //Drawing the line. 102 g.DrawLine(p, new Point(initX ?? e.X, initY ?? e.Y), new Point(e.X, e.Y)); 103 initX = e.X; 104 initY = e.Y; 105 */ 106 } 107 } 108 109 //Fired when the mouse pointer is over the pictureBox and mousebutton is released. 110 private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 111 { 112 //描画中を解除する 113 drawFlg = false; 114 115 startPaint = false; 116 initX = null; 117 initY = null; 118 } 119 /* 120 * 121 for (int x xy1.X; x <= xy2.X; x++) 122 { 123 RectangleF rect = new RectangleF( 124 x - (penWidth / 2), 125 y - (penWidth / 2), 126 penWidth,penWidth); 127 g.FillEllipse(brush, rect) 128 } 129 //Y軸基準で描画 130 float x = (float)xy1.X; 131 132 133 for (int y = xy1.Y; y <=xy2,Y; y++) 134 { 135 RectangleF rect = new RectangleF( 136 x - (penWidth / 2) 137 y - (penWhdth / 2) 138 penWidth, penWidth); 139 g.FillEllipse(Brush, rect); 140 x = x + a; 141 } 142 */ 143 144 //Button for Setting pen Color 145 private void btnPenColor1_Click(object sender, EventArgs e) 146 { 147 ColorDialog c = new ColorDialog(); 148 if(c.ShowDialog()==DialogResult.OK) 149 { 150 btnPenColor1.BackColor = c.Color; 151 } 152 } 153 //New 154 private void newToolStripMenuItem_Click(object sender, EventArgs e) 155 { 156 //Clearing the graphics from the Panel(pictureBox) 157 //g.Clear(pictureBox1.BackColor); 158 //Setting the BackColor of pictureBox and btnCanvasColor to white on Clicking New File Menu 159 pB1.BackColor = Color.White; 160 btnCanvasColor.BackColor = Color.White; 161 } 162 //Setting the Canvas Color 163 private void btnCanvasColor_Click(object sender, EventArgs e) 164 { 165 ColorDialog c = new ColorDialog(); 166 if(c.ShowDialog()==DialogResult.OK) 167 { 168 pB1.BackColor = c.Color; 169 btnCanvasColor.BackColor = c.Color; 170 } 171 } 172 private void drawSquare_Click(object sender, EventArgs e) 173 { 174 //drawSquare = true; 175 } 176 177 private void drawRectangle_Click(object sender, EventArgs e) 178 { 179 //drawRectangle = true; 180 } 181 182 private void drawCircle_Click(object sender, EventArgs e) 183 { 184 //drawCircle = true; 185 } 186 //Exit under File Menu 187 private void newToolStripMenuItem_Click_1(object sender, EventArgs e) 188 { 189 clear (); 190 } 191 private void clear() 192 { 193 using (Graphics g = Graphics.FromImage(bmp)) ; 194 g.FillRectangle(Brushes.White,new Rectangle(0,0, bmp.Width, bmp.Height)); 195 } 196 private void openToolStripMenuItem_Click(object sender, EventArgs e) 197 { 198 Image img = Bitmap.FromFile("test.png"); 199 bmp = new Bitmap(img); 200 pB1.Image = bmp; 201 } 202 private void exitToolStripMenuItem_Click(object sender, EventArgs e) 203 { 204 if(MessageBox.Show("Do you want to Exit?","Exit",MessageBoxButtons.YesNo,MessageBoxIcon.Information)==DialogResult.Yes) 205 { 206 Application.Exit(); 207 } 208 } 209 private void saveToolStripMenuItem_Click(object sender, EventArgs e) 210 { 211 bmp.Save("test.png", ImageFormat.Png); 212 } 213 //About under Help Menu 214 private void aboutToolStripMenuItem_Click(object sender, EventArgs e) 215 { 216 //About a = new About(); 217 //a.ShowDialog(); 218 } 219 220 //test 221 /* 222 private void btn1_Click(object sender, EventArgs e) 223 { 224 using (Graphics g = pB1.CreateGraphics()) ; 225 { 226 g.DrawLine(Pens.Blue, 100, 100, 300, 200); 227 using (Graphics g = Graphics.FromImage(bmp)) ; 228 { 229 g.DrawLine(Pens.Blue, 100, 100, 300, 200); 230 } 231 pB1.Image = bmp; 232 } 233 }*/ 234 } 235} 236

試したこと

サンプルコードを見ながら自分で打ち込む。
一から作り直して試す。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

解決ではない(たぶん)ですが、いくつか気になる点を。

C#

1 private void clear() 2 { 3 using (Graphics g = Graphics.FromImage(bmp)) ; 4 g.FillRectangle(Brushes.White,new Rectangle(0,0, bmp.Width, bmp.Height)); 5 } 6 7このusingで、g は開放されてしまうんじゃないかと 8やるなら、 9 10 using (Graphics g = Graphics.FromImage(bmp)){ 11 g.FillRectangle(Brushes.White,new Rectangle(0,0, bmp.Width, bmp.Height)); 12 } 13とするとかなんとか 14

んでもひとつ、

C#

1 //Button for Setting pen Color 2 private void btnPenColor1_Click(object sender, EventArgs e) 3 { 4 ColorDialog c = new ColorDialog(); 5 if(c.ShowDialog()==DialogResult.OK) 6 { 7 btnPenColor1.BackColor = c.Color; 8 } 9 } 10 11これだと、ColorDialogが残ったままになるので、 12 13 using( ColorDialog c = new ColorDialog()){ 14 なんやかや 15       } 16と、すべきかと

投稿2018/05/23 00:36

y_waiwai

総合スコア87749

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mistletoe

2018/05/23 01:05

回答ありがとうございます。 参考にさせていただきます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問