Image图片处理 GDI+绘图技术 .

 用到的知识点:
获取图片的缩略图
鼠标拽区,截取图片的指定区域
在Panel控件上绘图,绘制鼠标拽出来的矩形。双重缓存的运用。Invalidate、Paint重绘控件。
Bitmap,Image,Rectangle运用。 图片缩放。

 

程序代码 下载地址:http://download.csdn.net/source/633179

程序界面:

 

下面是主要代码:

'绘制图片 
2.    Private Sub panel1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Panel1.Paint
3.        MyBase.OnPaint(e)
4.        Me.InvokePaintBackground(Me.Panel1, e)
5.        If x0 < x1 Then
6.            RcDraw.X = x0
7.            RcDraw.Width = x1 - x0
8.        Else
9.            RcDraw.X = x1
10.            RcDraw.Width = x0 - x1
11.        End If
12.        If y0 < y1 Then
13.            RcDraw.Y = y0
14.            RcDraw.Height = y1 - y0
15.        Else
16.            RcDraw.Y = y1
17.            RcDraw.Height = y0 - y1
18.        End If
19.        If imageStatus Then
20.            WorkImg = New Bitmap(StartImg)
21.            Using gb As Graphics = Graphics.FromImage(WorkImg)
22.                RcDraw.Offset(-ImgBounds.X, -ImgBounds.Y)
23.                If RcDraw.X < 0 Then RcDraw.X = 0
24.                If RcDraw.Y < 0 Then RcDraw.Y = 0
25.                rcSel = RcDraw
26.                If RcDraw.X + RcDraw.Width >= ImgBounds.Width Then
27.                    RcDraw.Width = ImgBounds.Width - RcDraw.X - 1
28.                    rcSel.Width += 1
29.                End If
30.                If RcDraw.Y + RcDraw.Height >= ImgBounds.Height Then
31.                    RcDraw.Height = ImgBounds.Height - RcDraw.Y - 1
32.                    rcSel.Width += 1
33.                End If
34.                gb.DrawRectangle(pen, RcDraw)
35.            End Using
36.            e.Graphics.DrawImage(WorkImg, ImgBounds)
37.            'Using g As Graphics = e.Graphics 
38.            '    g.Clear(Color.White) 
39.            '    '重新画背景图 
40.            '    g.DrawImage(WorkImg, rcDest) 
41.            '    '画裁剪框 
42.            '    g.DrawRectangle(pen, RcDraw) 
43.            '    'Me.Panel1.BackgroundImage = WorkImg 
44.            'End Using 
45.        End If
46.    End Sub
47.    '选择图片 
48.    Private Sub BtOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtOpen.Click
49.        Dim fd As New OpenFileDialog()
50.        'fd.InitialDirectory = "D:/" 
51.        fd.Filter = "图像文件Image File|*.jpg;*.gif;*.png"
52.        If fd.ShowDialog() = Windows.Forms.DialogResult.OK Then
53.            CreateImage(fd.FileName)
54.            DrawImg()
55.        End If
56.    End Sub
57.    '打开图片 
58.    Private Sub CreateImage(ByVal path As String)
59.        If path.Equals(String.Empty) Then
60.            SrcImg = Nothing
61.            ShowMsg("空的图片路径。", MsgType.Err)
62.        Else
63.            SrcImg = New Bitmap(path)
64.            imageStatus = True
65.            ShowMsg("图片路径。" & path)
66.        End If
67.        CreateWorkingImage(SrcImg)
68.    End Sub
69.    '创建工作图片 
70.    Private Sub CreateWorkingImage(ByVal srcImage As Image)
71.        If Not (StartImg Is Nothing) Then
72.            StartImg.Dispose()
73.            StartImg = Nothing
74.        End If
75.        If Not (WorkImg Is Nothing) Then
76.            WorkImg.Dispose()
77.            WorkImg = Nothing
78.        End If
79.
80.        Dim maxSize As Integer = CInt(Math.Max(Me.Panel1.Width, Me.Panel1.Height))
81.        If srcImage.Width <= maxSize And srcImage.Height <= maxSize Then
82.            _scale = 1.0F
83.            StartImg = New Bitmap(srcImage)
84.        Else
85.            _scale = CSng(Math.Max(srcImage.Width, srcImage.Height)) / CSng(maxSize)
86.            Dim width As Integer = CInt(CSng(srcImage.Width) / _scale)
87.            Dim height As Integer = CInt(CSng(srcImage.Height) / _scale)
88.            StartImg = New Bitmap(width, height)
89.            Dim g As Graphics = Graphics.FromImage(StartImg)
90.            Try
91.                g.InterpolationMode = InterpolationMode.Bilinear
92.                Dim rcDest As New Rectangle(0, 0, width, height)
93.                Dim dsDest As New Rectangle(0, 0, srcImage.Width, srcImage.Height)
94.                g.DrawImage(srcImage, rcDest, dsDest, GraphicsUnit.Pixel)
95.            Finally
96.                g.Dispose()
97.            End Try
98.        End If
99.        WorkImg = New Bitmap(StartImg)
100.    End Sub
101.    '绘图工作图片 
102.    Private Sub DrawImg()
103.        Dim KrctWidth As Integer = Math.Max(Krct.Width, 0)
104.        Dim KrctHeight As Integer = Math.Max(Krct.Height, 0)
105.        Dim ratio As Single = Math.Max(CSng(WorkImg.Width / KrctWidth), CSng(WorkImg.Height / KrctHeight))
106.        Dim width As Integer = CInt(CSng(WorkImg.Width) / ratio)
107.        Dim height As Integer = CInt(CSng(WorkImg.Height) / ratio)
108.        Dim px, py As Integer
109.        px = (Math.Max(WorkImg.Width, KrctWidth) - Math.Min(WorkImg.Width, KrctWidth)) / 2
110.        py = (Math.Max(WorkImg.Height, KrctHeight) - Math.Min(WorkImg.Height, KrctHeight)) / 2
111.        ImgBounds = New Rectangle(px, py, WorkImg.Width, WorkImg.Height)
112.        Using g As Graphics = Panel1.CreateGraphics()
113.            g.DrawImage(WorkImg, ImgBounds)
114.        End Using
115.    End Sub

 

1.    '放大图片 
2.    Private Sub BtAddSize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtAddSize.Click
3.        Dim g As Graphics = Panel2.CreateGraphics()
4.        If SmallBmp IsNot Nothing Then
5.            resizeLevel = resizeLevel + 1
6.            Dim fac As Single = CSng((1 + (resizeLevel * 0.25)))
7.            Dim w As Integer = CInt((SmallBmp.Width * fac))
8.            Dim h As Integer = CInt((SmallBmp.Height * fac))
9.            Dim rd As New Rectangle(0, 0, w, h)
10.            Dim tempBmp As New Bitmap(w, h)
11.            Dim gi As Graphics = Graphics.FromImage(tempBmp)
12.            gi.DrawImage(SmallBmp, rd)
13.            g.DrawImage(tempBmp, rd)
14.            gi.Dispose()
15.        End If
16.        g.Dispose()
17.
18.    End Sub
19.
20.    '缩小图片 
21.    Private Sub BtMinSize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtMinSize.Click
22.        Dim g As Graphics = Panel2.CreateGraphics()
23.        If SmallBmp IsNot Nothing Then
24.            resizeLevel = IIf((resizeLevel > -3), resizeLevel - 1, resizeLevel)
25.            Dim fac As Single = CSng((1 + (resizeLevel * 0.25)))
26.            Dim w As Integer = CInt((SmallBmp.Width * fac))
27.            Dim h As Integer = CInt((SmallBmp.Height * fac))
28.            Dim rd As New Rectangle(0, 0, w, h)
29.            Dim tempBmp As New Bitmap(w, h)
30.            Dim gi As Graphics = Graphics.FromImage(tempBmp)
31.            g.FillRectangle(Brushes.White, Panel2.ClientRectangle)
32.            gi.DrawImage(SmallBmp, rd)
33.            g.DrawImage(tempBmp, rd)
34.            gi.Dispose()
35.        End If
36.        g.Dispose()
37.
38.    End Sub



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值