天天看點

原創企業級控件庫之圖檔浏覽控件

這篇我将給大家介紹:企業級控件庫之圖檔浏覽控件。

  摘要

  我想大家用過或聽說過ACDSee 對于圖檔浏覽的強大功能,我接下來介紹的控件與ACDSee相比,可謂是天壤之别,雖沒有其強大的功能,但用在一些常用的軟體上,提供一些常用的基本功能還是可以的。同時,我隻提供一個模子,代碼開源,你可以随便修改以滿足自己的需要。

  成就别人、等于成就自己。我沒什麼要求,歡迎大家多多支援與評論,覺得不錯的,記得點選文章左下角的”關注部落格”,就這麼簡單。同時,你要用什麼好的想法,也可以與我交流,謝謝。

  圖檔浏覽控件運作效果如下圖:

  

  本控件類圖:

  本控件類詳細資訊:

  本控件核心代碼: 

滾動滑鼠滾輪實作滑鼠縮放

1 #region 滾動滑鼠滾輪實作滑鼠縮放

2 /************************************************************

3 * 滾動滑鼠滾輪實作滑鼠縮放

4 ************************************************************/

5 private void picView_MouseWheel(object sender, MouseEventArgs e)

6 {

7 switch (keyAction)

8 {

9 case 1:

10 if (e.Delta > 0 && picView.Width < 10000)

11 {

12 zoom(e.Location, 1100);

13 }

14 else if (e.Delta < 0 && picView.Image.Width / picView.Width < 5)

15 {

16 zoom(e.Location, 900);

17 }

18 CenterImage();//使圖檔居中顯示

19   break;

20 case 2:

21 if (hScrollBarImageView.Visible)

22 hScrollBarImageView.Value = (hScrollBarImageView.Value - e.Delta < 0 ? 0 : (hScrollBarImageView.Value - e.Delta > hScrollBarImageView.Maximum ? hScrollBarImageView.Maximum : hScrollBarImageView.Value - e.Delta));

23 break;

24 default:

25 if (vScrollBarImageView.Visible)

26 vScrollBarImageView.Value = (vScrollBarImageView.Value - e.Delta < 0 ? 0 : (vScrollBarImageView.Value - e.Delta > vScrollBarImageView.Maximum ? vScrollBarImageView.Maximum : vScrollBarImageView.Value - e.Delta));

27 break;

28 }

29 }

30 #endregion

  下面給出整個控件的完整代碼及窗體調用方法:

  一、控件完整代碼:

圖像顯示控件

1 #region 版權資訊

2 /*---------------------------------------------------------------------*

3 // Copyright (C) 2010 http://www.cnblogs.com/huyong

4 // 版權所有。

5 // 項目 名稱:《Winform通用控件庫》

6 // 文 件 名: UcImageView.cs

7 // 類 全 名: DotNet.Controls.UcImageView

8 // 描 述: 圖像顯示控件

9 // 建立 時間: 2010-08-05

10 // 建立人資訊: [**** 姓名:胡勇 QQ:80368704 E-Mail:[email protected] *****]

11 *----------------------------------------------------------------------*/

12 #endregion

13

14 using System;

15 using System.ComponentModel;

16 using System.Drawing;

17 using System.Windows.Forms;

18 using System.Drawing.Printing;

19 using DotNet.Common;

20

21 namespace DotNet.Controls

22 {

23 /// <summary>

24 /// 圖像顯示控件

25 /// UcImageView

26 ///

27 /// 修改紀錄

28 /// 2010-11-6 胡勇 優化相關代碼。

29 /// 2010-8-5 胡勇 建立圖像顯示控件

30 ///

31 /// <author>

32 /// <name>胡勇</name>

33 /// <QQ>80368704</QQ>

34 /// <Email>[email protected]</Email>

35 /// </author>

36 /// </summary>

37 public partial class UcImageView : UserControl

38 {

39 #region 構造函數

40 public UcImageView()

41 {

42 InitializeComponent();

43 //記錄PnlMain的size

44 panelOldSize.X = PnlMain.Width;

45 panelOldSize.Y = PnlMain.Height;

46 }

47 #endregion

48

49 #region 公共變量

50 private Point StartP = new Point(0, 0);

51 private bool isMouseDown = false;

52 private Point panelOldSize = new Point(0, 0);

53 private int imgIndexBy1000 = 0;

54 private int keyAction = 0;

55 private int w, h;

56 #endregion

57

58 #region 公共事件

59

60 [Category("圖檔浏覽"), Description("移動或漫遊圖檔的Checked事件Changed時發生。"), Browsable(true)]

61 public event EventHandler OnMnuMoveImageCheckedChanged;

62

63 #endregion

64

65 #region 公共方法

66

67 #region 增加圖檔到PictureBox:void AddImage(string fileName, bool isAsync)

68 /// <summary>

69 /// 增加要顯示的圖檔

70 /// </summary>

71 /// <param name="fileName">圖檔路徑全名</param>

72 /// <param name="isAsync">true:異步方式加載圖檔</param>

73 public void AddImage(string fileName, bool isAsync)

74 {

75 if (isAsync)//異步加載圖檔

76 {

77 //圖檔異步加載完成後的處理事件

78 picView.LoadCompleted += new AsyncCompletedEventHandler(picView_LoadCompleted);

79 //圖檔加裁時,顯示等待光标

80 picView.UseWaitCursor = true;

81 //采用異步加裁方式

82 picView.WaitOnLoad = false;

83 //開始異步加裁圖檔

84 picView.LoadAsync(fileName);

85 }

86 else

87 {

88 picView.Image = Image.FromFile(fileName);//載入圖檔

89 }

90

91 InitialImage();

92

93 }

94

95 /// <summary>

96 /// 增加要顯示的圖檔

97 /// </summary>

98 /// <param name="img">Image</param>

99 public void AddImage(Image img)

100 {

101 if (img != null)

102 {

103 picView.Image = img;

104 InitialImage();

105 }

106 else

107 {

108 picView = null;

109 }

110 }

111 #endregion

112

113 #region 得到ImageView中的圖檔:Image GetImageInImageView()

114 /// <summary>

115 /// 得到ImageView中的圖檔

116 /// </summary>

117 /// <returns>Image</returns>

118 public Image GetImageInImageView()

119 {

120 if (picView.Image != null)

121 {

122 return picView.Image;

123 }

124 else

125 {

126 return null;

127 }

128 }

129 #endregion

130

131 #region 放大、縮小、适應圖檔大小、移動圖檔、左旋轉圖檔與右旋轉圖檔

132 /// <summary>

133 /// 放大圖檔

134 /// </summary>

135 public void ZoomInImage()

136 {

137 if (picView.Image != null)

138 {

139 if (picView.Width < 5000)

140 {

141 zoom(picView.Location, 1100);

142 CenterImage();

143 }

144 else

145 {

146 MessageBox.Show("對不起,不能再進行放大!","提示資訊",MessageBoxButtons.OK,MessageBoxIcon.Information);

147 }

148 }

149 }

150

151 /// <summary>

152 /// 縮小圖檔

153 /// </summary>

154 public void ZoomOutImage()

155 {

156 if (picView.Image != null)

157 {

158 if (picView.Image.Width / picView.Width < 5 && -7 < 0)

159 {

160 zoom(picView.Location, 900);

161 CenterImage();

162 }

163 else

164 {

165 MessageBox.Show("對不起,不能再進行縮小!", "提示資訊", MessageBoxButtons.OK, MessageBoxIcon.Information);

166 }

167 }

168 }

169

170 /// <summary>

171 /// 适應圖檔大小

172 /// </summary>

173 public void FitImageSize()

174 {

175 if (picView.Image != null)

176 {

177 float r1 = (float)this.w / this.picView.Width;

178 float r2 = (float)this.h / this.picView.Height;

179 this.picView.Scale(r1, r2);

180 this.picView.Left = (this.PnlMain.Width - this.picView.Width) / 2;

181 this.picView.Top = (this.PnlMain.Height - this.picView.Height) / 2;

182 this.picView.Cursor = Cursors.Default;

183 CenterImage();

184 }

185 }

186

187 /// <summary>

188 /// 移動圖檔

189 /// </summary>

190 public void MoveImage()

191 {

192 mnuMy.Checked = MnuMoveImageChecked;

193 }

194

195 /// <summary>

196 /// 左旋轉圖檔

197 /// </summary>

198 public void LeftRotateImage()

199 {

200 if (picView.Image != null)

201 {

202 picView.Image.RotateFlip(RotateFlipType.Rotate90FlipX);

203 picView.Refresh();

204 CenterImage();

205 }

206 }

207

208 /// <summary>

209 /// 右旋轉圖檔

210 /// </summary>

211 public void RightRotateImage()

212 {

213 if (picView.Image != null)

214 {

215 picView.Image.RotateFlip(RotateFlipType.Rotate90FlipY);

216 picView.Refresh();

217 CenterImage();

218 }

219 }

220 #endregion

221

222 #endregion

223

224 #region 公共屬性

225 private bool _mnuMoveImageChecked;

226 /// <summary>

227 /// 确定漫遊菜單項是否處于選中狀态(用于是否可以移動或漫遊圖檔)

228 /// </summary>

229 [Category("圖檔浏覽"), Description("确定漫遊菜單項是否處于選中狀态(用于是否可以移動或漫遊圖檔)"),Browsable(false)]

230 public bool MnuMoveImageChecked

231 {

232 get

233 {

234 return _mnuMoveImageChecked;

235 }

236 set

237 {

238 _mnuMoveImageChecked = value;

239 this.mnuMy.Checked = _mnuMoveImageChecked;

240 }

241 }

242

243 private bool _mnuPrintVisible = true; //預設可見

244 /// <summary>

245 /// 确定列印菜單項是可見還是隐藏

246 /// </summary>

247 [Category("圖檔浏覽"), Description("确定列印菜單項是可見還是隐藏"), Browsable(true)]

248 public bool MnuPrintVisible

249 {

250 get

251 {

252 return _mnuPrintVisible;

253 }

254 set

255 {

256 _mnuPrintVisible = value;

257 this.mnuPrint.Visible = _mnuPrintVisible;

258 }

259 }

260 #endregion

261

262 #region 私有方法

263

264 private void picView_LoadCompleted(object sender, AsyncCompletedEventArgs e)

265 {

266 //圖檔加載完成後,将光标恢複

267 picView.UseWaitCursor = false;

268 }

269

270 #region 圖檔縮放

271 /// <summary>

272 /// 圖檔縮放

273 /// </summary>

274 /// <param name="center">縮放中心點</param>

275 /// <param name="zoomIndexBy1000">縮放倍率的1000倍</param>

276 private void zoom(Point center, int zoomIndexBy1000)

277 {

278 //記錄原始的picView的Size

279 Point oldSize = new Point(picView.Width, picView.Height);

280 //實施放大(以x方向為基準計算得出y方向大小,防止多次運算誤差積累使Image和picView的尺寸不比對)

281 picView.Width = picView.Width * zoomIndexBy1000 / 1000;

282 picView.Height = picView.Width * imgIndexBy1000 / 1000;

283 //重新定位标定後的picView位置

284 picView.Left -= ((picView.Width - oldSize.X) * (center.X * 1000 / oldSize.X)) / 1000;

285 picView.Top -= ((picView.Height - oldSize.Y) * (center.Y * 1000 / oldSize.Y)) / 1000;

286 //重新設定橫向滾動條最大值和位置

287 if (picView.Width - PnlMain.Width > 0)

288 {

289 hScrollBarImageView.Visible = true;

290 hScrollBarImageView.Maximum = picView.Width - PnlMain.Width + vScrollBarImageView.Width + 2;

291 hScrollBarImageView.Value = (picView.Left >= 0 ? 0 : (-picView.Left > hScrollBarImageView.Maximum ? hScrollBarImageView.Maximum : -picView.Left));

292 }

293 else

294 {

295 hScrollBarImageView.Visible = false;

296 }

297 //重新設定縱向滾動條最大值和位置

298 if (picView.Height - PnlMain.Height > 0)

299 {

300 vScrollBarImageView.Visible = true;

301 vScrollBarImageView.Maximum = picView.Height - PnlMain.Height + hScrollBarImageView.Width + 2;

302 vScrollBarImageView.Value = (picView.Top >= 0 ? 0 : (-picView.Top > vScrollBarImageView.Maximum ? vScrollBarImageView.Maximum : -picView.Top));

303 }

304 else

305 {

306 vScrollBarImageView.Visible = false;

307 }

308 }

309 #endregion

310

311 #region 圖檔加裁到PictureBox中後,對其進行初始化操作

312 /// <summary>

313 /// 圖檔加裁到PictureBox中後,對其進行初始化操作

314 /// </summary>

315 private void InitialImage()

316 {

317 ImageChange();//得到最适合顯示的圖檔尺寸

318 this.w = this.picView.Width;

319 this.h = this.picView.Height;

320

321 //設定圖檔位置

322 picView.Location = new Point(0, 0);

323 //設定圖檔初始尺寸

324 picView.Size = picView.Image.Size;

325 //設定圖檔縱橫比

326 imgIndexBy1000 = (picView.Image.Height * 1000) / picView.Image.Width;

327 //設定滾動條

328 if (picView.Width - PnlMain.Width > 0)

329 {

330 hScrollBarImageView.Maximum = picView.Width - PnlMain.Width + vScrollBarImageView.Width + 2;//+ hScrollBarImageView.LargeChange

331 hScrollBarImageView.Visible = true;

332 }

333 if (picView.Height - PnlMain.Height > 0)

334 {

335 vScrollBarImageView.Maximum = picView.Height - PnlMain.Height + hScrollBarImageView.Height + 2;//+ vScrollBarImageView.LargeChange

336 vScrollBarImageView.Visible = true;

337 }

338 CenterImage();

339 }

340 #endregion

341

342 #region 居中與全屏顯示圖檔

343

344 /// <summary>

345 /// 使圖檔全屏顯示

346 /// </summary>

347 private void FullImage()

348 {

349 if (picView.Image.Width < picView.Width && picView.Image.Height < picView.Height)

350 {

351 picView.SizeMode = PictureBoxSizeMode.CenterImage;

352 }

353 CalculateAspectRatioAndSetDimensions();

354 }

355

356

357 /// <summary>

358 /// 保持圖檔居中顯示

359 /// </summary>

360 private void CenterImage()

361 {

362 picView.Left = PnlMain.Width / 2 - picView.Width / 2;

363 picView.Top = PnlMain.Height / 2 - picView.Height / 2;

364 }

365

366 #endregion

367

368 #region CalculateAspectRatioAndSetDimensions

369 /// <summary>

370 /// CalculateAspectRatioAndSetDimensions

371 /// </summary>

372 /// <returns>double</returns>

373 private double CalculateAspectRatioAndSetDimensions()

374 {

375 double ratio;

376 if (picView.Image.Width > picView.Image.Height)

377 {

378 ratio = picView.Image.Width / picView.Image.Height;

379 picView.Height = Convert.ToInt16(double.Parse(picView.Width.ToString()) / ratio);

380 }

381 else

382 {

383 ratio = picView.Image.Height / picView.Image.Width;

384 picView.Width = Convert.ToInt16(double.Parse(picView.Height.ToString()) / ratio);

385 }

386 return ratio;

387 }

388 #endregion

389

390 #region 用于适應圖檔大小

391 /// <summary>

392 /// 用于适應圖檔大小

393 /// </summary>

394 private void ImageChange()

395 {

396 this.picView.Height = this.picView.Image.Height;

397 this.picView.Width = this.picView.Image.Width;

398 float cx = 1;

399 if (this.picView.Image.Height > this.PnlMain.Height) cx =

400 (float)(this.PnlMain.Height - 10) / (float)this.picView.Image.Height;

401

402 this.picView.Scale(cx);

403 this.picView.Left = (this.PnlMain.Width - this.picView.Width) / 2;

404 this.picView.Top = (this.PnlMain.Height - this.picView.Height) / 2;

405 }

406 #endregion

407

408 #endregion

409

410 #region 視窗(PnlMain)尺寸改變時圖像的顯示位置控制

411 /************************************************************

412 * 視窗(PnlMain)尺寸改變時圖像的顯示位置控制

413 ************************************************************/

414 private void PnlMain_Resize(object sender, EventArgs e)

415 {

416 //對左右的方向操作(左右)

417 if (picView.Width <= PnlMain.Width) //圖檔左右居中

418 {

419 picView.Left = (PnlMain.Width - picView.Width) / 2;

420 hScrollBarImageView.Visible = false;

421 }

422 else if (picView.Left < 0 && picView.Width + picView.Left < PnlMain.Width)//圖檔靠右

423 {

424 picView.Left = PnlMain.Width - picView.Width;

425 hScrollBarImageView.Visible = true;

426 }

427 else if (picView.Left > 0 && picView.Width + picView.Left > PnlMain.Width)//圖檔靠左

428 {

429 picView.Left = 0;

430 hScrollBarImageView.Visible = true;

431 }

432 else//保證顯示的中心圖樣不變(左右)

433 {

434 picView.Left += (PnlMain.Width - panelOldSize.X) / 2;

435 hScrollBarImageView.Visible = true;

436 }

437 //設定橫向滾動條最大值

438 hScrollBarImageView.Maximum = (picView.Width - PnlMain.Width > 0 ? picView.Width - PnlMain.Width + hScrollBarImageView.Maximum + 2 : 0);

439 //設定橫向滾動條Value

440 hScrollBarImageView.Value = (picView.Left >= 0 ? 0 : -picView.Left);

441 //重置舊的pannel1的Width

442 panelOldSize.X = PnlMain.Width;

443

444 //對上下的方向操作(上下)

445 if (picView.Height <= PnlMain.Height)//圖檔上下居中

446 {

447 picView.Top = (PnlMain.Height - picView.Height) / 2;

448 vScrollBarImageView.Visible = false;

449 }

450 else if (picView.Top < 0 && picView.Height + picView.Top < PnlMain.Height)//圖檔靠下

451 {

452 picView.Top = PnlMain.Height - picView.Height;

453 vScrollBarImageView.Visible = true;

454 }

455 else if (picView.Top > 0 && picView.Height + picView.Top > PnlMain.Height)//圖檔靠上

456 {

457 picView.Top = 0;

458 vScrollBarImageView.Visible = true;

459 }

460 else//保證顯示的中心圖樣不變(上下)

461 {

462 picView.Top += (PnlMain.Height - panelOldSize.Y) / 2;

463 vScrollBarImageView.Visible = true;

464 }

465 //設定縱向滾動條最大值

466 vScrollBarImageView.Maximum = (picView.Height - PnlMain.Height > 0 ? picView.Height - PnlMain.Height + vScrollBarImageView.Maximum + 2 : 0);

467 //設定縱向滾動條Value

468 vScrollBarImageView.Value = (picView.Top >= 0 ? 0 : -picView.Top);

469 //重置舊的pannel1的Height

470 panelOldSize.Y = PnlMain.Height;

471 }

472 #endregion

473

474 #region 滾動條滾動時,圖檔移動

475 /************************************************************

476 * 滾動條滾動時,圖檔移動

477 ************************************************************/

478 private void vScrollBarImageView_ValueChanged(object sender, EventArgs e)

479 {

480 picView.Top = -vScrollBarImageView.Value;

481 }

482

483 private void hScrollBarImageView_ValueChanged(object sender, EventArgs e)

484 {

485 picView.Left = -hScrollBarImageView.Value;

486 }

487 #endregion

488

489 #region PictureBox 滑鼠按下、滑鼠進入、松開與移動事件

490 private void picView_MouseDown(object sender, MouseEventArgs e)

491 {

492 StartP = e.Location;

493 isMouseDown = true;

494 }

495

496 private void picView_MouseEnter(object sender, EventArgs e)

497 {

498 picView.Focus();

499 }

500

501 private void picView_MouseMove(object sender, MouseEventArgs e)

502 {

503 if (mnuMy.Checked && isMouseDown)

504 {

505 this.picView.Cursor = Cursors.SizeAll;

506 //計算出移動後兩個滾動條應該的Value

507 int x = -picView.Left + StartP.X - e.X;

508 int y = -picView.Top + StartP.Y - e.Y;

509

510 //如果滾動條的value有效則執行操作;

511 if (x >= -PnlMain.Width + 10 && x <= picView.Width - 10)

512 {

513 if (hScrollBarImageView.Visible)

514 {

515 if (x > 0)

516 hScrollBarImageView.Value = x > hScrollBarImageView.Maximum ? hScrollBarImageView.Maximum : x;

517 picView.Left = -x - (vScrollBarImageView.Visible && x < 0 ? vScrollBarImageView.Width : 0);

518 }

519 else

520 picView.Left = -x;

521 }

522

523 if (y >= -PnlMain.Height + 10 && y <= picView.Height - 10)

524 {

525 if (vScrollBarImageView.Visible)

526 {

527 if (y > 0)

528 vScrollBarImageView.Value = y > vScrollBarImageView.Maximum ? vScrollBarImageView.Maximum : y;

529 picView.Top = -y - (hScrollBarImageView.Visible && y < 0 ? hScrollBarImageView.Height : 0);

530 }

531 else

532 picView.Top = -y;

533 }

534

535

536 /*****************************************************

537 * 給予調整滾動條調整圖檔位置

538 *****************************************************

539 計算出移動後兩個滾動條應該的Value*/

540 /*int w = hScrollBarImageView.Value + StartP.X -e.X;

541 int z = vScrollBarImageView.Value + StartP.Y -e.Y;

542 如果滾動條的value有效則執行操作;

543 否則将滾動條按不同情況拉到兩頭

544 if (w >= 0 && w <= hScrollBarImageView.Maximum)

545 {

546 hScrollBarImageView.Value = w;

547 }

548 else

549 {

550 hScrollBarImageView.Value = (w < 0 ? 0 : hScrollBarImageView.Maximum);

551 }

552 if (z >= 0 && z <= vScrollBarImageView.Maximum)

553 {

554 vScrollBarImageView.Value = z;

555 }

556 else

557 {

558 vScrollBarImageView.Value = (z < 0 ? 0 : vScrollBarImageView.Maximum);

559 }*/

560 }

561 else

562 {

563 this.picView.Cursor = Cursors.Default;

564 }

565 }

566

567 private void picView_MouseUp(object sender, MouseEventArgs e)

568 {

569 isMouseDown = false;

570 }

571 #endregion

572

573 #region 滾動滑鼠滾輪實作滑鼠縮放

574 /************************************************************

575 * 滾動滑鼠滾輪實作滑鼠縮放

576 ************************************************************/

577 private void picView_MouseWheel(object sender, MouseEventArgs e)

578 {

579 switch (keyAction)

580 {

581 case 1:

582 if (e.Delta > 0 && picView.Width < 10000)

583 {

584 zoom(e.Location, 1100);

585 }

586 else if (e.Delta < 0 && picView.Image.Width / picView.Width < 5)

587 {

588 zoom(e.Location, 900);

589 }

590 CenterImage();//使圖檔居中顯示

591 break;

592 case 2:

593 if (hScrollBarImageView.Visible)

594 hScrollBarImageView.Value = (hScrollBarImageView.Value - e.Delta < 0 ? 0 : (hScrollBarImageView.Value - e.Delta > hScrollBarImageView.Maximum ? hScrollBarImageView.Maximum : hScrollBarImageView.Value - e.Delta));

595 break;

596 default:

597 if (vScrollBarImageView.Visible)

598 vScrollBarImageView.Value = (vScrollBarImageView.Value - e.Delta < 0 ? 0 : (vScrollBarImageView.Value - e.Delta > vScrollBarImageView.Maximum ? vScrollBarImageView.Maximum : vScrollBarImageView.Value - e.Delta));

599 break;

600 }

601 }

602 #endregion

603

604 #region 窗體按鍵事件處理

605

606 private void UcImageView_KeyDown(object sender, KeyEventArgs e)

607 {

608 if (e.Control)

609 keyAction = 1;

610 else if (e.Shift)

611 keyAction = 2;

612 }

613

614 private void UcImageView_KeyUp(object sender, KeyEventArgs e)

615 {

616 keyAction = 0;

617 }

618

619 private void picView_KeyDown(object sender, KeyEventArgs e)

620 {

621 if (e.Control)

622 keyAction = 1;

623 else if (e.Shift)

624 keyAction = 2;

625 else

626 keyAction = 3;

627 }

628 private void picView_KeyUp(object sender, KeyEventArgs e)

629 {

630 keyAction = 0;

631 }

632 #endregion

633

634 #region 快捷菜單事件代碼

635 //放大圖檔

636 private void mnuZoomIn_Click(object sender, EventArgs e)

637 {

638 this.ZoomInImage();

639 }

640

641 //縮小圖檔

642 private void mnuZoomOut_Click(object sender, EventArgs e)

643 {

644 this.ZoomOutImage();

645 }

646

647 //适應圖檔大小

648 private void mnuFitSize_Click(object sender, EventArgs e)

649 {

650 this.FitImageSize();

651 }

652

653 //漫遊圖檔

654 private void mnuMy_Click(object sender, EventArgs e)

655 {

656 MnuMoveImageChecked = !mnuMy.Checked;

657 this.MoveImage();

658 }

659

660 //左旋轉圖檔

661 private void mnuLeftRotate_Click(object sender, EventArgs e)

662 {

663 this.LeftRotateImage();

664 }

665

666 //右旋轉圖檔

667 private void mnuRightRotate_Click(object sender, EventArgs e)

668 {

669 this.RightRotateImage();

670 }

671

672 #region 列印圖檔

673 //列印圖檔

674 private void mnuPrint_Click(object sender, EventArgs e)

675 {

676 if (picView.Image != null)

677 {

678 PrintDialog printDgImageView = new PrintDialog();

679

680 /*設定紙張類型

681 PaperSize pg = new PaperSize("A3",297,420);

682 printDocImageView.DefaultPageSettings.PaperSize = pg;*/

683 PaperSize pg = new PaperSize("A3", 23, 33);

684 printDgImageView.Document = printDocImageView;

685 if (printDgImageView.ShowDialog() == DialogResult.OK)

686 {

687 try

688 {

689 printDocImageView.Print();

690 }

691 catch

692 {

693 //停止列印

694 printDocImageView.PrintController.OnEndPrint(printDocImageView, new System.Drawing.Printing.PrintEventArgs());

695 }

696 }

697 }

698 else

699 {

700 DialogHelper.ShowWarningMsg("對不起,沒有要列印的資料!");

701 }

702 }

703

704 private void printDocImageView_PrintPage(object sender, PrintPageEventArgs e)

705 {

706 this.FitImageSize();//實際尺寸

707 if (picView.Image != null)

708 {

709 e.Graphics.DrawImage(picView.Image, picView.Location.X, picView.Location.Y, picView.Width, picView.Height);

710 }

711 }

712 #endregion

713

714 #endregion

715

716 #region 窗體事件

717 private void mnuMy_CheckedChanged(object sender, EventArgs e)

718 {

719 if (OnMnuMoveImageCheckedChanged != null)

720 {

721 MnuMoveImageChecked = this.mnuMy.Checked;

722 OnMnuMoveImageCheckedChanged(sender, EventArgs.Empty);

723 }

724 }

725 #endregion

726 }

727 }

本文轉自yonghu86 51CTO部落格,原文連結:http://blog.51cto.com/yonghu/1321374,如需轉載請自行聯系原作者

繼續閱讀