[VB.NET]VB.NET中用drawImage()画图以后怎么保存?程序代码如下:

VB.NET源码-156个实用实例哦…… VB.NET中用drawImage()画图以后怎么保存?程序代码如下:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim ofd As New SaveFileDialog

Private pictureBox1 As New PictureBox


#Region Windows Form Designer generated code

Public Sub New()
MyBase.New()

This call is required by the Windows Form Designer.
InitializeComponent()

Add any initialization after the InitializeComponent() call

End Sub

Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

NOTE: The following procedure is required by the Windows Form Designer
It can be modified using the Windows Form Designer.
Do not modify it using the code editor.
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
Private Sub InitializeComponent()
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.MenuItem1 = New System.Windows.Forms.MenuItem
Me.MenuItem2 = New System.Windows.Forms.MenuItem
Me.MenuItem3 = New System.Windows.Forms.MenuItem
Me.SuspendLayout()

PictureBox1

Me.PictureBox1.Dock = System.Windows.Forms.DockStyle.Fill
Me.PictureBox1.Location = New System.Drawing.Point(0, 0)
Me.PictureBox1.Name = PictureBox1
Me.PictureBox1.Size = New System.Drawing.Size(392, 358)
Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False

MainMenu1

Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})

MenuItem1

Me.MenuItem1.Index = 0
Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem3})
Me.MenuItem1.Text = file

MenuItem2

Me.MenuItem2.Index = 0
Me.MenuItem2.Text = save

MenuItem3

Me.MenuItem3.Index = 1
Me.MenuItem3.Text =

Form1

Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(392, 358)
Me.Controls.Add(Me.PictureBox1)
Me.Menu = Me.MainMenu1
Me.Name = Form1
Me.Text = Form1
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dock the PictureBox to the form and set its background to white.
pictureBox1.Dock = DockStyle.Fill
pictureBox1.BackColor = Color.White
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
Connect the Paint event of the PictureBox to the event handling method.
AddHandler pictureBox1.Paint, AddressOf Me.pictureBox1_Paint


Add the PictureBox control to the Form.
Me.Controls.Add(pictureBox1)

End Sub
Private Sub pictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
Create image.
Dim newImage1 As Image = Image.FromFile( F:/lianxi/Image1--1.jpg )
Dim newImage2 As Image = Image.FromFile( F:/lianxi/Image1--2.jpg )
Dim newImage3 As Image = Image.FromFile( F:/lianxi/Image2--1.jpg )
Dim newImage4 As Image = Image.FromFile( F:/lianxi/Image2--2.jpg )
Create rectangle for displaying image.
Dim destRect As New Rectangle(0, 0, 520, 520)

Create rectangle for source image.
Dim srcRect As New Rectangle(0, 0, 520, 520)
Dim destRect1 As New Rectangle(520, 0, 520, 520)
Dim destRect2 As New Rectangle(0, 520, 520, 520)
Dim destRect3 As New Rectangle(520, 520, 520, 520)

Dim rec As Rectangle

Dim units As GraphicsUnit = GraphicsUnit.Pixel
Draw image to screen.
e.Graphics.DrawImage(newImage1, destRect, srcRect, units)
e.Graphics.DrawImage(newImage2, destRect1, srcRect, units)
e.Graphics.DrawImage(newImage3, destRect2, srcRect, units)
e.Graphics.DrawImage(newImage4, destRect3, srcRect, units)

End Sub pictureBox1_Paint

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
ofd.Filter = jpg file|*.jpg|bmp file|*.bmp


Dim bmp As New Bitmap(1040, 1040)
bmp = Me.PictureBox1.Image


If (ofd.ShowDialog = DialogResult.OK) Then
bmp.Save(ofd.FileName)
End If

End Sub
End Class
每次运行到 bmp.Save(ofd.FileName)
这一步时就出现An unhandled exception of type System.NullReferenceException occurred in WindowsApplication4.exe

Additional information: Object reference not set to an instance of an object.查看了下,原来Me.PictureBox1.Image为nothing,这样传值给bmp时,bmp也是nothing.想了下,最终的问题归结为用DrawImage()函数以后,居然没有把图象画到PictureBox1中,但是可以在PictureBox1中显示,也不知道是怎么回事.
在线等回复.
__________________________________________________________________________
帮顶下,学习中....
__________________________________________________________________________
看一下ofd是否为空引用。 然后bmp.Save(file) 中file参数应该是你要保存到的路径。
__________________________________________________________________________
Dim ofd As New SaveFileDialog这句已经定义,ofd不会为空,关键是bmp显示到了控件PictureBox1中,但是PictureBox1的属性项Image为空,也真够郁闷的,画了图象进去却不能保存.
__________________________________________________________________________
bmp.save的时候第一个写名字,第二个一定要 imageformat.xx,即保存的格式,否则就保存为位图
__________________________________________________________________________
低效
__________________________________________________________________________
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 tesseract 进行 OCR 的 VB.NET 代码示例: ```vb Imports System Imports System.IO Imports System.Drawing Imports System.Drawing.Imaging Imports System.Diagnostics Imports System.Runtime.InteropServices Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim inputFile As String = "test.png" Dim outputFile As String = "output.txt" Dim tesseractPath As String = "C:\Program Files (x86)\Tesseract-OCR\tesseract.exe" ' Convert the input image to grayscale Dim bmp As New Bitmap(inputFile) Dim grayBmp As New Bitmap(bmp.Width, bmp.Height, Imaging.PixelFormat.Format8bppIndexed) Using g As Graphics = Graphics.FromImage(grayBmp) Dim colorMatrix As New ColorMatrix(New Single()() _ {New Single() {0.3F, 0.3F, 0.3F, 0, 0}, New Single() {0.59F, 0.59F, 0.59F, 0, 0}, New Single() {0.11F, 0.11F, 0.11F, 0, 0}, New Single() {0, 0, 0, 1, 0}, New Single() {0, 0, 0, 0, 1}}) Dim ia As New ImageAttributes() ia.SetColorMatrix(colorMatrix) g.DrawImage(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height), _ 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, ia) End Using ' Save the grayscale image to a file grayBmp.Save("temp.tif", Imaging.ImageFormat.Tiff) ' Run tesseract on the grayscale image Dim processInfo As New ProcessStartInfo() processInfo.FileName = tesseractPath processInfo.Arguments = "temp.tif " & outputFile & " -l eng" processInfo.CreateNoWindow = True processInfo.UseShellExecute = False processInfo.RedirectStandardOutput = True processInfo.RedirectStandardError = True Dim process As Process = Process.Start(processInfo) process.WaitForExit() ' Load the output text file and display the result Dim outputText As String = File.ReadAllText(outputFile) MessageBox.Show(outputText) ' Delete the temporary files File.Delete("temp.tif") File.Delete(outputFile) End Sub End Class ``` 请注意,您需要在计算机上安装 tesseract OCR 引擎,并相应地更新 `tesseractPath` 变量。此外,您也可以根据需要更改语言设置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值