用到的知识点:
获取图片的缩略图
鼠标拽区,截取图片的指定区域
在Panel控件上绘图,绘制鼠标拽出来的矩形。双重缓存的运用。Invalidate、Paint重绘控件。
Bitmap,Image,Rectangle运用。 图片缩放。
获取图片的缩略图
鼠标拽区,截取图片的指定区域
在Panel控件上绘图,绘制鼠标拽出来的矩形。双重缓存的运用。Invalidate、Paint重绘控件。
Bitmap,Image,Rectangle运用。 图片缩放。
程序代码 下载地址:http://download.csdn.net/source/633179
程序界面:
--
--
下面是主要代码:
- '绘制图片
- Private Sub panel1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Panel1.Paint
- MyBase.OnPaint(e)
- Me.InvokePaintBackground(Me.Panel1, e)
- If x0 < x1 Then
- RcDraw.X = x0
- RcDraw.Width = x1 - x0
- Else
- RcDraw.X = x1
- RcDraw.Width = x0 - x1
- End If
- If y0 < y1 Then
- RcDraw.Y = y0
- RcDraw.Height = y1 - y0
- Else
- RcDraw.Y = y1
- RcDraw.Height = y0 - y1
- End If
- If imageStatus Then
- WorkImg = New Bitmap(StartImg)
- Using gb As Graphics = Graphics.FromImage(WorkImg)
- RcDraw.Offset(-ImgBounds.X, -ImgBounds.Y)
- If RcDraw.X < 0 Then RcDraw.X = 0
- If RcDraw.Y < 0 Then RcDraw.Y = 0
- rcSel = RcDraw
- If RcDraw.X + RcDraw.Width >= ImgBounds.Width Then
- RcDraw.Width = ImgBounds.Width - RcDraw.X - 1
- rcSel.Width += 1
- End If
- If RcDraw.Y + RcDraw.Height >= ImgBounds.Height Then
- RcDraw.Height = ImgBounds.Height - RcDraw.Y - 1
- rcSel.Width += 1
- End If
- gb.DrawRectangle(pen, RcDraw)
- End Using
- e.Graphics.DrawImage(WorkImg, ImgBounds)
- 'Using g As Graphics = e.Graphics
- ' g.Clear(Color.White)
- ' '重新画背景图
- ' g.DrawImage(WorkImg, rcDest)
- ' '画裁剪框
- ' g.DrawRectangle(pen, RcDraw)
- ' 'Me.Panel1.BackgroundImage = WorkImg
- 'End Using
- End If
- End Sub
- '选择图片
- Private Sub BtOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtOpen.Click
- Dim fd As New OpenFileDialog()
- 'fd.InitialDirectory = "D:/"
- fd.Filter = "图像文件Image File|*.jpg;*.gif;*.png"
- If fd.ShowDialog() = Windows.Forms.DialogResult.OK Then
- CreateImage(fd.FileName)
- DrawImg()
- End If
- End Sub
- '打开图片
- Private Sub CreateImage(ByVal path As String)
- If path.Equals(String.Empty) Then
- SrcImg = Nothing
- ShowMsg("空的图片路径。", MsgType.Err)
- Else
- SrcImg = New Bitmap(path)
- imageStatus = True
- ShowMsg("图片路径。" & path)
- End If
- CreateWorkingImage(SrcImg)
- End Sub
- '创建工作图片
- Private Sub CreateWorkingImage(ByVal srcImage As Image)
- If Not (StartImg Is Nothing) Then
- StartImg.Dispose()
- StartImg = Nothing
- End If
- If Not (WorkImg Is Nothing) Then
- WorkImg.Dispose()
- WorkImg = Nothing
- End If
- Dim maxSize As Integer = CInt(Math.Max(Me.Panel1.Width, Me.Panel1.Height))
- If srcImage.Width <= maxSize And srcImage.Height <= maxSize Then
- _scale = 1.0F
- StartImg = New Bitmap(srcImage)
- Else
- _scale = CSng(Math.Max(srcImage.Width, srcImage.Height)) / CSng(maxSize)
- Dim width As Integer = CInt(CSng(srcImage.Width) / _scale)
- Dim height As Integer = CInt(CSng(srcImage.Height) / _scale)
- StartImg = New Bitmap(width, height)
- Dim g As Graphics = Graphics.FromImage(StartImg)
- Try
- g.InterpolationMode = InterpolationMode.Bilinear
- Dim rcDest As New Rectangle(0, 0, width, height)
- Dim dsDest As New Rectangle(0, 0, srcImage.Width, srcImage.Height)
- g.DrawImage(srcImage, rcDest, dsDest, GraphicsUnit.Pixel)
- Finally
- g.Dispose()
- End Try
- End If
- WorkImg = New Bitmap(StartImg)
- End Sub
- '绘图工作图片
- Private Sub DrawImg()
- Dim KrctWidth As Integer = Math.Max(Krct.Width, 0)
- Dim KrctHeight As Integer = Math.Max(Krct.Height, 0)
- Dim ratio As Single = Math.Max(CSng(WorkImg.Width / KrctWidth), CSng(WorkImg.Height / KrctHeight))
- Dim width As Integer = CInt(CSng(WorkImg.Width) / ratio)
- Dim height As Integer = CInt(CSng(WorkImg.Height) / ratio)
- Dim px, py As Integer
- px = (Math.Max(WorkImg.Width, KrctWidth) - Math.Min(WorkImg.Width, KrctWidth)) / 2
- py = (Math.Max(WorkImg.Height, KrctHeight) - Math.Min(WorkImg.Height, KrctHeight)) / 2
- ImgBounds = New Rectangle(px, py, WorkImg.Width, WorkImg.Height)
- Using g As Graphics = Panel1.CreateGraphics()
- g.DrawImage(WorkImg, ImgBounds)
- End Using
- End Sub
- '放大图片
- Private Sub BtAddSize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtAddSize.Click
- Dim g As Graphics = Panel2.CreateGraphics()
- If SmallBmp IsNot Nothing Then
- resizeLevel = resizeLevel + 1
- Dim fac As Single = CSng((1 + (resizeLevel * 0.25)))
- Dim w As Integer = CInt((SmallBmp.Width * fac))
- Dim h As Integer = CInt((SmallBmp.Height * fac))
- Dim rd As New Rectangle(0, 0, w, h)
- Dim tempBmp As New Bitmap(w, h)
- Dim gi As Graphics = Graphics.FromImage(tempBmp)
- gi.DrawImage(SmallBmp, rd)
- g.DrawImage(tempBmp, rd)
- gi.Dispose()
- End If
- g.Dispose()
- End Sub
- '缩小图片
- Private Sub BtMinSize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtMinSize.Click
- Dim g As Graphics = Panel2.CreateGraphics()
- If SmallBmp IsNot Nothing Then
- resizeLevel = IIf((resizeLevel > -3), resizeLevel - 1, resizeLevel)
- Dim fac As Single = CSng((1 + (resizeLevel * 0.25)))
- Dim w As Integer = CInt((SmallBmp.Width * fac))
- Dim h As Integer = CInt((SmallBmp.Height * fac))
- Dim rd As New Rectangle(0, 0, w, h)
- Dim tempBmp As New Bitmap(w, h)
- Dim gi As Graphics = Graphics.FromImage(tempBmp)
- g.FillRectangle(Brushes.White, Panel2.ClientRectangle)
- gi.DrawImage(SmallBmp, rd)
- g.DrawImage(tempBmp, rd)
- gi.Dispose()
- End If
- g.Dispose()
- End Sub