用vb.net划一条直线,并保存为bmp图片文件
vb.net吧
全部回复
仅看楼主
level 5
dzweather 楼主
界面:Form PictureBox Button
操作:点击Button时,在PictureBox上画一条直线显示,同时将PictureBox中图形保存为
Bmp图片文件
如何做呢?
======================================================
下面是我做错的:
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim gr As Graphics = PictureBox1.CreateGraphics
Dim
bp
As New Bitmap(PictureBox1.Width, PictureBox1.Height, gr)
gr.DrawLine(Pens.Black, 10, 10, 50, 50)
PictureBox1.DrawToBitmap(bp, New Rectangle(0, 0, 70, 70))
bp.Save("D:\1.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
gr.Dispose()
bp.Dispose()
End Sub
End Class
结果:
错误原因在哪里?
2013年09月27日 07点09分 1
level 5
dzweather 楼主
百度很久,思前想后。
原来bitmap创建时默认就是黑色背景。
于是代码如下:
Imports System.DrawingImports System.Drawing.Drawing2DPublic Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim gr As Graphics Dim bp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
Dim pen1 As New Pen(Color.Red)
pen1.DashStyle = DashStyle.DashDotDot
pen1.Width = 2
PictureBox1.Image = bp
gr = Graphics.FromImage(PictureBox1.Image)
gr.FillRectangle(Brushes.White, New Rectangle(0, 0, PictureBox1.Width, PictureBox1.Height))
gr.DrawLine(pen1, 10, 10, 70, 70)
PictureBox1.Image.Save("D:\1.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
gr.Dispose()
End Sub
End Class
2013年09月28日 14点09分 2
1