原创企业级控件库之图片浏览控件

原创企业级控件库之图片浏览控件

发布日期:2010年12月18日星期六作者:EricHu

 

  在上两篇:我介绍了原创企业级控件库之组合查询控件 原创企业级控件库之大数据量分页控件,受到了很多朋友的支持,给了我很大的动力,在此我特表感谢。有的朋友要求把源码提供上来,我在第一篇就讲了,源码会在我这个系列讲完之后提供,大家先别着急,如果你确实需要,可以时常关注此系列,谢谢大家。其实,在系列文章中,我已把核心代码贡献出来了。学习有时是参考别人与实践别人的劳动成果的过程,你光把别人的代码拿过来用用,不研究其实质,进步很慢。

这篇我将给大家介绍:企业级控件库之图片浏览控件。

  摘要

  我想大家用过或听说过ACDSee 对于图片浏览的强大功能,我接下来介绍的控件与ACDSee相比,可谓是天壤之别,虽没有其强大的功能,但用在一些常用的软件上,提供一些常用的基本功能还是可以的。同时,我只提供一个模子,代码开源,你可以随便修改以满足自己的需要。

  成就别人、等于成就自己。我没什么要求,欢迎大家多多支持与评论,觉得不错的,记得点击文章左下角的”关注博客”,就这么简单。同时,你要用什么好的想法,也可以与我交流,谢谢。

  图片浏览控件运行效果如下图:

  

  本控件类图:

  本控件类详细信息:

  本控件核心代码: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#region 滚动鼠标滚轮实现鼠标缩放
/************************************************************
         * 滚动鼠标滚轮实现鼠标缩放
         ************************************************************/
privatevoid picView_MouseWheel(object sender, MouseEventArgs e)
        {
switch (keyAction)
            {
case1:
if (e.Delta >0&& picView.Width <10000)
                    {
                        zoom(e.Location, 1100);
                    }
elseif (e.Delta <0&& picView.Image.Width / picView.Width <5)
                    {
                        zoom(e.Location, 900);
                    }
                    CenterImage();//使图片居中显示
 break;
case2:
if (hScrollBarImageView.Visible)
                        hScrollBarImageView.Value = (hScrollBarImageView.Value - e.Delta <0?0 : (hScrollBarImageView.Value - e.Delta > hScrollBarImageView.Maximum ? hScrollBarImageView.Maximum : hScrollBarImageView.Value - e.Delta));
break;
default:
if (vScrollBarImageView.Visible)
                        vScrollBarImageView.Value = (vScrollBarImageView.Value - e.Delta <0?0 : (vScrollBarImageView.Value - e.Delta > vScrollBarImageView.Maximum ? vScrollBarImageView.Maximum : vScrollBarImageView.Value - e.Delta));
break;
            }
        }
#endregion  

  下面给出整个控件的完整代码及窗体调用方法:

  一、控件完整代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
#region  版权信息
/*---------------------------------------------------------------------*
// Copyright (C) 2010 http://www.cnblogs.com/huyong
// 版权所有。
// 项目  名称:《Winform通用控件库》
// 文  件  名: UcImageView.cs
// 类  全  名: DotNet.Controls.UcImageView
// 描      述:  图像显示控件
// 创建  时间: 2010-08-05
// 创建人信息: [**** 姓名:胡勇 QQ:406590790 E-Mail:406590790@qq.com *****]
*----------------------------------------------------------------------*/
#endregion
 
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
using DotNet.Common;
 
namespace DotNet.Controls
{
///<summary>
/// 图像显示控件
/// UcImageView
///
/// 修改纪录
///     2010-11-6  胡勇 优化相关代码。
///     2010-8-5   胡勇 创建图像显示控件
///    
///<author>
///<name>胡勇</name>
///<QQ>80368704</QQ>
///<Email>80368704@qq.com</Email>
///</author>
///</summary>
publicpartialclass UcImageView : UserControl
    {
#region 构造函数
public UcImageView()
        {
            InitializeComponent();
//记录PnlMain的size
            panelOldSize.X = PnlMain.Width;
            panelOldSize.Y = PnlMain.Height;
        }
#endregion
 
#region 公共变量
private Point StartP =new Point(0, 0);
privatebool isMouseDown =false;
private Point panelOldSize =new Point(0, 0);
privateint imgIndexBy1000 =0;
privateint keyAction =0;
privateint w, h;
#endregion
 
#region 公共事件
 
        [Category("图片浏览"), Description("移动或漫游图片的Checked事件Changed时发生。"), Browsable(true)]
publicevent EventHandler OnMnuMoveImageCheckedChanged;
 
#endregion
 
#region 公共方法
 
#region 增加图片到PictureBox:void AddImage(string fileName, bool isAsync)
///<summary>
/// 增加要显示的图片
///</summary>
///<param name="fileName">图片路径全名</param>
///<param name="isAsync">true:异步方式加载图片</param>
publicvoid AddImage(string fileName, bool isAsync)
        {
if (isAsync)//异步加载图片
            {
//图片异步加载完成后的处理事件
                picView.LoadCompleted +=new AsyncCompletedEventHandler(picView_LoadCompleted);
//图片加裁时,显示等待光标
                picView.UseWaitCursor =true;
//采用异步加裁方式
                picView.WaitOnLoad =false;
//开始异步加裁图片
                picView.LoadAsync(fileName);
            }
else
            {
                picView.Image = Image.FromFile(fileName);//载入图片
            }
 
            InitialImage();
            
        }
 
///<summary>
/// 增加要显示的图片
///</summary>
///<param name="img">Image</param>
publicvoid AddImage(Image img)
        {
if (img !=null)
            {
                picView.Image = img;
                InitialImage();
            }
else
            {
                picView =null;
            }
        }
#endregion
 
#region 得到ImageView中的图片:Image GetImageInImageView()
///<summary>
/// 得到ImageView中的图片
///</summary>
///<returns>Image</returns>
public Image GetImageInImageView()
        {
if (picView.Image !=null)
            {
return picView.Image;
            }
else
            {
returnnull;
            }
        }
#endregion
 
#region 放大、缩小、适应图片大小、移动图片、左旋转图片与右旋转图片
///<summary>
/// 放大图片
///</summary>
publicvoid ZoomInImage()
        {
if (picView.Image !=null)
            {
if (picView.Width <5000)
                {
                    zoom(picView.Location, 1100);
                    CenterImage();
                }
else
                {
                    MessageBox.Show("对不起,不能再进行放大!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
            }
        }
         
///<summary>
/// 缩小图片
///</summary>
publicvoid ZoomOutImage()
        {
if (picView.Image !=null)
            {
if (picView.Image.Width / picView.Width <5&&-7<0)
                {
                    zoom(picView.Location, 900);
                    CenterImage();
                }
else
                {
                    MessageBox.Show("对不起,不能再进行缩小!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
 
///<summary>
/// 适应图片大小
///</summary>
publicvoid FitImageSize()
        {
if (picView.Image !=null)
            {
float r1 = (float)this.w /this.picView.Width;
float r2 = (float)this.h /this.picView.Height;
this.picView.Scale(r1, r2);
this.picView.Left = (this.PnlMain.Width -this.picView.Width) /2;
this.picView.Top = (this.PnlMain.Height -this.picView.Height) /2;
this.picView.Cursor = Cursors.Default;
                CenterImage();
            }
        }
 
///<summary>
/// 移动图片
///</summary>
publicvoid MoveImage()
        {
            mnuMy.Checked = MnuMoveImageChecked;
        }
 
///<summary>
/// 左旋转图片
///</summary>
publicvoid LeftRotateImage()
        {
if (picView.Image !=null)
            {
                picView.Image.RotateFlip(RotateFlipType.Rotate90FlipX);
                picView.Refresh();
                CenterImage();
            }
        }
 
///<summary>
/// 右旋转图片
///</summary>
publicvoid RightRotateImage()
        {
if (picView.Image !=null)
            {
                picView.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                picView.Refresh();
                CenterImage();
            }
        }
#endregion
 
#endregion
 
#region 公共属性
privatebool _mnuMoveImageChecked;
///<summary>
/// 确定漫游菜单项是否处于选中状态(用于是否可以移动或漫游图片)
///</summary>
        [Category("图片浏览"), Description("确定漫游菜单项是否处于选中状态(用于是否可以移动或漫游图片)"),Browsable(false)]
publicbool MnuMoveImageChecked
        {
get
            {
return _mnuMoveImageChecked;
            }
set
            {
                _mnuMoveImageChecked = value;
this.mnuMy.Checked = _mnuMoveImageChecked;
            }
        }
 
privatebool _mnuPrintVisible =true; //默认可见
///<summary>
/// 确定打印菜单项是可见还是隐藏
///</summary>
        [Category("图片浏览"), Description("确定打印菜单项是可见还是隐藏"), Browsable(true)]
publicbool MnuPrintVisible
        {
get
            {
return _mnuPrintVisible;
            }
set
            {
                _mnuPrintVisible = value;
this.mnuPrint.Visible = _mnuPrintVisible;
            }
        }
#endregion
 
#region 私有方法
 
privatevoid picView_LoadCompleted(object sender, AsyncCompletedEventArgs e)
        {
//图片加载完成后,将光标恢复
            picView.UseWaitCursor =false;
        }
 
#region 图片缩放
///<summary>
/// 图片缩放
///</summary>
///<param name="center">缩放中心点</param>
///<param name="zoomIndexBy1000">缩放倍率的1000倍</param>
privatevoid zoom(Point center, int zoomIndexBy1000)
        {
//记录原始的picView的Size
            Point oldSize =new Point(picView.Width, picView.Height);
//实施放大(以x方向为基准计算得出y方向大小,防止多次运算误差积累使Image和picView的尺寸不匹配)
            picView.Width = picView.Width * zoomIndexBy1000 /1000;
            picView.Height = picView.Width * imgIndexBy1000 /1000;
//重新定位标定后的picView位置
            picView.Left -= ((picView.Width - oldSize.X) * (center.X *1000/ oldSize.X)) /1000;
            picView.Top -= ((picView.Height - oldSize.Y) * (center.Y *1000/ oldSize.Y)) /1000;
//重新设定横向滚动条最大值和位置
if (picView.Width - PnlMain.Width >0)
            {
                hScrollBarImageView.Visible =true;
                hScrollBarImageView.Maximum = picView.Width - PnlMain.Width + vScrollBarImageView.Width +2;
                hScrollBarImageView.Value = (picView.Left >=0?0 : (-picView.Left > hScrollBarImageView.Maximum ? hScrollBarImageView.Maximum : -picView.Left));
            }
else
            {
                hScrollBarImageView.Visible =false;
            }
//重新设定纵向滚动条最大值和位置
if (picView.Height - PnlMain.Height >0)
            {
                vScrollBarImageView.Visible =true;
                vScrollBarImageView.Maximum = picView.Height - PnlMain.Height + hScrollBarImageView.Width +2;
                vScrollBarImageView.Value = (picView.Top >=0?0 : (-picView.Top > vScrollBarImageView.Maximum ? vScrollBarImageView.Maximum : -picView.Top));
            }
else
            {
                vScrollBarImageView.Visible =false;
            }
        }
#endregion
 
#region 图片加裁到PictureBox中后,对其进行初始化操作
///<summary>
/// 图片加裁到PictureBox中后,对其进行初始化操作
///</summary>
privatevoid InitialImage()
        {
            ImageChange();//得到最适合显示的图片尺寸
this.w =this.picView.Width;
this.h =this.picView.Height;
 
//设定图片位置
            picView.Location =new Point(0, 0);
//设定图片初始尺寸
            picView.Size = picView.Image.Size;
//设定图片纵横比
            imgIndexBy1000 = (picView.Image.Height *1000) / picView.Image.Width;
//设定滚动条
if (picView.Width - PnlMain.Width >0)
            {
                hScrollBarImageView.Maximum = picView.Width - PnlMain.Width + vScrollBarImageView.Width +2;//+ hScrollBarImageView.LargeChange
                hScrollBarImageView.Visible =true;
            }
if (picView.Height - PnlMain.Height >0)
            {
                vScrollBarImageView.Maximum = picView.Height - PnlMain.Height + hScrollBarImageView.Height +2;//+ vScrollBarImageView.LargeChange
                vScrollBarImageView.Visible =true;
            }
            CenterImage();
        }
#endregion
 
#region 居中与全屏显示图片
 
///<summary>
/// 使图片全屏显示
///</summary>
privatevoid FullImage()
        {
if (picView.Image.Width < picView.Width && picView.Image.Height < picView.Height)
            {
                picView.SizeMode = PictureBoxSizeMode.CenterImage;
            }
            CalculateAspectRatioAndSetDimensions();
        }
 
 
///<summary>
/// 保持图片居中显示
///</summary>
privatevoid CenterImage()
        {
            picView.Left = PnlMain.Width /2- picView.Width /2;
            picView.Top = PnlMain.Height /2- picView.Height /2;
        }
 
#endregion
 
#region CalculateAspectRatioAndSetDimensions
///<summary>
/// CalculateAspectRatioAndSetDimensions
///</summary>
///<returns>double</returns>
privatedouble CalculateAspectRatioAndSetDimensions()
        {
double ratio;
if (picView.Image.Width > picView.Image.Height)
            {
                ratio = picView.Image.Width / picView.Image.Height;
                picView.Height = Convert.ToInt16(double.Parse(picView.Width.ToString()) / ratio);
            }
else
            {
                ratio = picView.Image.Height / picView.Image.Width;
                picView.Width = Convert.ToInt16(double.Parse(picView.Height.ToString()) / ratio);
            }
return ratio;
        }
#endregion
 
#region 用于适应图片大小
///<summary>
/// 用于适应图片大小
///</summary>
privatevoid ImageChange()
        {
this.picView.Height =this.picView.Image.Height;
this.picView.Width =this.picView.Image.Width;
float cx =1;
if (this.picView.Image.Height >this.PnlMain.Height) cx =
                (float)(this.PnlMain.Height -10) / (float)this.picView.Image.Height;
 
this.picView.Scale(cx);
this.picView.Left = (this.PnlMain.Width -this.picView.Width) /2;
this.picView.Top = (this.PnlMain.Height -this.picView.Height) /2;
        }
#endregion
 
#endregion
 
#region 窗口(PnlMain)尺寸改变时图像的显示位置控制
/************************************************************
         * 窗口(PnlMain)尺寸改变时图像的显示位置控制
         ************************************************************/
privatevoid PnlMain_Resize(object sender, EventArgs e)
        {
//对左右的方向操作(左右)
if (picView.Width <= PnlMain.Width) //图片左右居中
            {
                picView.Left = (PnlMain.Width - picView.Width) /2;
                hScrollBarImageView.Visible =false;
            }
elseif (picView.Left <0&& picView.Width + picView.Left < PnlMain.Width)//图片靠右
            {
                picView.Left = PnlMain.Width - picView.Width;
                hScrollBarImageView.Visible =true;
            }
elseif (picView.Left >0&& picView.Width + picView.Left > PnlMain.Width)//图片靠左
            {
                picView.Left =0;
                hScrollBarImageView.Visible =true;
            }
else//保证显示的中心图样不变(左右)
            {
                picView.Left += (PnlMain.Width - panelOldSize.X) /2;
                hScrollBarImageView.Visible =true;
            }
//设定横向滚动条最大值
            hScrollBarImageView.Maximum = (picView.Width - PnlMain.Width >0? picView.Width - PnlMain.Width + hScrollBarImageView.Maximum +2 : 0);
//设定横向滚动条Value
            hScrollBarImageView.Value = (picView.Left >=0?0 : -picView.Left);
//重置旧的pannel1的Width
            panelOldSize.X = PnlMain.Width;
 
//对上下的方向操作(上下)
if (picView.Height <= PnlMain.Height)//图片上下居中
            {
                picView.Top = (PnlMain.Height - picView.Height) /2;
                vScrollBarImageView.Visible =false;
            }
elseif (picView.Top <0&& picView.Height + picView.Top < PnlMain.Height)//图片靠下
            {
                picView.Top = PnlMain.Height - picView.Height;
                vScrollBarImageView.Visible =true;
            }
elseif (picView.Top >0&& picView.Height + picView.Top > PnlMain.Height)//图片靠上
            {
                picView.Top =0;
                vScrollBarImageView.Visible =true;
            }
else//保证显示的中心图样不变(上下)
            {
                picView.Top += (PnlMain.Height - panelOldSize.Y) /2;
                vScrollBarImageView.Visible =true;
            }
//设定纵向滚动条最大值
            vScrollBarImageView.Maximum = (picView.Height - PnlMain.Height >0? picView.Height - PnlMain.Height + vScrollBarImageView.Maximum +2 : 0);
//设定纵向滚动条Value
            vScrollBarImageView.Value = (picView.Top >=0?0 : -picView.Top);
//重置旧的pannel1的Height
            panelOldSize.Y = PnlMain.Height;
        }
#endregion
 
#region 滚动条滚动时,图片移动
/************************************************************
         * 滚动条滚动时,图片移动
         ************************************************************/
privatevoid vScrollBarImageView_ValueChanged(object sender, EventArgs e)
        {
            picView.Top =-vScrollBarImageView.Value;
        }
 
privatevoid hScrollBarImageView_ValueChanged(object sender, EventArgs e)
        {
            picView.Left =-hScrollBarImageView.Value;
        }
#endregion
 
#region PictureBox 鼠标按下、鼠标进入、松开与移动事件
privatevoid picView_MouseDown(object sender, MouseEventArgs e)
        {
            StartP = e.Location;
            isMouseDown =true;
        }
 
privatevoid picView_MouseEnter(object sender, EventArgs e)
        {
            picView.Focus();
        }
 
privatevoid picView_MouseMove(object sender, MouseEventArgs e)
        {
if (mnuMy.Checked && isMouseDown)
            {
this.picView.Cursor = Cursors.SizeAll;
//计算出移动后两个滚动条应该的Value
int x =-picView.Left + StartP.X - e.X;
int y =-picView.Top + StartP.Y - e.Y;
 
//如果滚动条的value有效则执行操作;
if (x >=-PnlMain.Width +10&& x <= picView.Width -10)
                {
if (hScrollBarImageView.Visible)
                    {
if (x >0)
                            hScrollBarImageView.Value = x > hScrollBarImageView.Maximum ? hScrollBarImageView.Maximum : x;
                        picView.Left =-x - (vScrollBarImageView.Visible && x <0? vScrollBarImageView.Width : 0);
                    }
else
                        picView.Left =-x;
                }
                 
if (y >=-PnlMain.Height +10&& y <= picView.Height -10)
                {
if (vScrollBarImageView.Visible)
                    {
if (y >0)
                            vScrollBarImageView.Value = y > vScrollBarImageView.Maximum ? vScrollBarImageView.Maximum : y;
                        picView.Top =-y - (hScrollBarImageView.Visible && y <0? hScrollBarImageView.Height : 0);
                    }
else
                        picView.Top =-y;
                }
 
 
/*****************************************************
                 * 给予调整滚动条调整图片位置
                 *****************************************************
                计算出移动后两个滚动条应该的Value*/
/*int w = hScrollBarImageView.Value + StartP.X -e.X;
                int z = vScrollBarImageView.Value + StartP.Y -e.Y;
                如果滚动条的value有效则执行操作;
                否则将滚动条按不同情况拉到两头
                if (w >= 0 && w <= hScrollBarImageView.Maximum)
                {
                    hScrollBarImageView.Value = w;
                }
                else
                {
                    hScrollBarImageView.Value = (w < 0 ? 0 : hScrollBarImageView.Maximum);
                }
                if (z >= 0 && z <= vScrollBarImageView.Maximum)
                {
                    vScrollBarImageView.Value = z;
                }
                else
                {
                    vScrollBarImageView.Value = (z < 0 ? 0 : vScrollBarImageView.Maximum);
                }*/
            }
else
            {
this.picView.Cursor = Cursors.Default;
            }
        }
 
privatevoid picView_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown =false;
        }
#endregion
 
#region 滚动鼠标滚轮实现鼠标缩放
/************************************************************
         * 滚动鼠标滚轮实现鼠标缩放
         ************************************************************/
privatevoid picView_MouseWheel(object sender, MouseEventArgs e)
        {
switch (keyAction)
            {
case1:
if (e.Delta >0&& picView.Width <10000)
                    {
                        zoom(e.Location, 1100);
                    }
elseif (e.Delta <0&& picView.Image.Width / picView.Width <5)
                    {
                        zoom(e.Location, 900);
                    }
                    CenterImage();//使图片居中显示
break;
case2:
if (hScrollBarImageView.Visible)
                        hScrollBarImageView.Value = (hScrollBarImageView.Value - e.Delta <0?0 : (hScrollBarImageView.Value - e.Delta > hScrollBarImageView.Maximum ? hScrollBarImageView.Maximum : hScrollBarImageView.Value - e.Delta));
break;
default:
if (vScrollBarImageView.Visible)
                        vScrollBarImageView.Value = (vScrollBarImageView.Value - e.Delta <0?0 : (vScrollBarImageView.Value - e.Delta > vScrollBarImageView.Maximum ? vScrollBarImageView.Maximum : vScrollBarImageView.Value - e.Delta));
break;
            }
        }
#endregion
 
#region 窗体按键事件处理
 
privatevoid UcImageView_KeyDown(object sender, KeyEventArgs e)
        {
if (e.Control)
                keyAction =1;
elseif (e.Shift)
                keyAction =2;
        }
 
privatevoid UcImageView_KeyUp(object sender, KeyEventArgs e)
        {
            keyAction =0;
        }
 
privatevoid picView_KeyDown(object sender, KeyEventArgs e)
        {
if (e.Control)
                keyAction =1;
elseif (e.Shift)
                keyAction =2;
else
                keyAction =3;
        }
privatevoid picView_KeyUp(object sender, KeyEventArgs e)
        {
            keyAction =0;
        }
#endregion
 
#region 快捷菜单事件代码
//放大图片
privatevoid mnuZoomIn_Click(object sender, EventArgs e)
        {
this.ZoomInImage();
        }
 
//缩小图片
privatevoid mnuZoomOut_Click(object sender, EventArgs e)
        {
this.ZoomOutImage();
        }
 
//适应图片大小
privatevoid mnuFitSize_Click(object sender, EventArgs e)
        {
this.FitImageSize();
        }
 
//漫游图片
privatevoid mnuMy_Click(object sender, EventArgs e)
        {
            MnuMoveImageChecked =!mnuMy.Checked;
this.MoveImage();
        }
 
//左旋转图片
privatevoid mnuLeftRotate_Click(object sender, EventArgs e)
        {
this.LeftRotateImage();
        }
 
//右旋转图片
privatevoid mnuRightRotate_Click(object sender, EventArgs e)
        {
this.RightRotateImage();
        }
 
#region 打印图片
//打印图片
privatevoid mnuPrint_Click(object sender, EventArgs e)
        {
if (picView.Image !=null)
            {
                PrintDialog printDgImageView =new PrintDialog();
 
/*设置纸张类型
                PaperSize pg = new PaperSize("A3",297,420);
                printDocImageView.DefaultPageSettings.PaperSize = pg;*/
                PaperSize pg =new PaperSize("A3", 23, 33);
                printDgImageView.Document = printDocImageView;
if (printDgImageView.ShowDialog() == DialogResult.OK)
                {
try
                    {
                        printDocImageView.Print();
                    }
catch
                    {
//停止打印
                        printDocImageView.PrintController.OnEndPrint(printDocImageView, new System.Drawing.Printing.PrintEventArgs());
                    }
                }
            }
else
            {
                DialogHelper.ShowWarningMsg("对不起,没有要打印的数据!");
            }
        }
 
privatevoid printDocImageView_PrintPage(object sender, PrintPageEventArgs e)
        {
this.FitImageSize();//实际尺寸
if (picView.Image !=null)
            {
                e.Graphics.DrawImage(picView.Image, picView.Location.X, picView.Location.Y, picView.Width, picView.Height);
            }
        }
#endregion
 
#endregion
 
#region 窗体事件
privatevoid mnuMy_CheckedChanged(object sender, EventArgs e)
        {
if (OnMnuMoveImageCheckedChanged !=null)
            {
                MnuMoveImageChecked =this.mnuMy.Checked;
                OnMnuMoveImageCheckedChanged(sender, EventArgs.Empty);
            }
        }
#endregion
    }
}  

  二、WinForm窗体调用方法完整代码: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DotNet.Common;
 
namespace DotNet.WinForm.Example
{
publicpartialclass FrmUcImageView : Form
    {
public FrmUcImageView()
        {
            InitializeComponent();
        }
 
 
privatevoid FrmUcImageView_Shown(object sender, EventArgs e)
        {
try
            {
                ucImageViewTest.AddImage(Image.FromFile(Application.StartupPath +"\\windows7.jpg"));
            }
catch(Exception ex)
            {
                DialogHelper.ShowErrorMsg(ex.Message);
            }
        }
 
privatevoid btnAdd_Click(object sender, EventArgs e)
        {
            openImgDlg.Filter ="Image Files(*.jpg;*.jpeg;*.tiff;*.gif)|*.jpg;*.gpeg;*.tiff;*.gif|All files (*.*)|*.*";//图片文件的类型
if (openImgDlg.ShowDialog() == DialogResult.OK)
            {
string FileName, pathName;
                pathName = openImgDlg.FileName;
                FileName = FileHelper.GetName(pathName);
                ucImageViewTest.AddImage(Image.FromFile(pathName));
            }
        }
 
privatevoid btnZoomIn_Click(object sender, EventArgs e)
        {
            ucImageViewTest.ZoomInImage();
        }
 
privatevoid btnZoomOut_Click(object sender, EventArgs e)
        {
            ucImageViewTest.ZoomOutImage();
        }
 
privatevoid btnMy_Click(object sender, EventArgs e)
        {
            btnMy.Checked = ucImageViewTest.MnuMoveImageChecked =!btnMy.Checked;
            ucImageViewTest.MoveImage();
        }
 
privatevoid btnFitSize_Click(object sender, EventArgs e)
        {
            ucImageViewTest.FitImageSize();
        }
 
privatevoid btnLeftRotate_Click(object sender, EventArgs e)
        {
            ucImageViewTest.LeftRotateImage();
        }
 
privatevoid btnRightRotate_Click(object sender, EventArgs e)
        {
            ucImageViewTest.RightRotateImage();
        }
 
privatevoid ucImageViewTest_OnMnuMoveImageCheckedChanged(object sender, EventArgs e)
        {
//使ucImageViewDj中的快捷菜单中的漫游的选中状态与当前窗口漫游按钮的选中状态保持一至
            btnMy.Checked = ucImageViewTest.MnuMoveImageChecked;
        }
 
privatevoid btnExit_Click(object sender, EventArgs e)
        {
this.Close();
        }
 
    }
}  

 

posted @   .NET快速开发框架  阅读(7065)  评论(22编辑  收藏  举报
编辑推荐:
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
阅读排行:
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验
点击右上角即可分享
微信分享提示