我正在使用 VB 6.0 开发一个库存管理项目。现在我想包含一个功能,允许用户将他的项目的照片上传到我的系统。然后我想将它保存到数据库中。此外,我想将照片尺寸限制为 600*600。因此,当用户上传大于 600*600 像素的图片时,我的系统应自动调整照片大小以适合我的图片框。任何人都可以帮忙吗?提前致谢。
问问题
8669 次
2 回答
0
没有简单的方法可以做到这一点。我将利用 VB 标准控件的副作用。取一个表单/UserControl/whatever,并坚持以下控件:
- 图片控件<imgPic>
- 图片控件<imgSize>
- 图片框控件 <pctCanvas>
我添加了 <dlgOpen> 只是为了创建一个文件打开对话框。
<imgPic> 仅用于预览图片。<imgSize> 用于轻松获取图片文件的宽度和高度(以像素为单位)(请注意,我将表单的 ScaleMode 属性设置为 vbPixels 以便更轻松地执行此操作)。<pctCanvas> 纯粹是用来让我调整图片的大小。
添加以下代码:
Option Explicit
' Edit these if you change your mind about the photo size.
Private Const m_ksngMaxPixelsX As Single = 600!
Private Const m_ksngMaxPixelsY As Single = 600!
Private Sub cmdLoad_Click()
On Error GoTo ErrorHandler:
dlgOpen.Filter = "Picture Files|*.bmp;*.jpg;*.gif"
dlgOpen.ShowOpen
UploadPicture dlgOpen.FileName
Exit Sub
ErrorHandler:
Err.Raise Err.Number, Err.Source, Err.Description
End Sub
Private Sub Form_Load()
Me.ScaleMode = vbPixels
pbCanvas.ScaleMode = vbPixels
imgPic.Width = m_ksngMaxPixelsX
imgPic.Height = m_ksngMaxPixelsY
imgSize.Visible = False
pbCanvas.Visible = False
pbCanvas.AutoRedraw = True
End Sub
' Get a new filename, based on the original. It will always be a BMP bitmap.
Private Function GetResizedFilename(ByRef the_sFilename As String) As String
Dim nPosDot As Long
On Error GoTo ErrorHandler
nPosDot = InStrRev(the_sFilename, ".")
GetResizedFilename = Left$(the_sFilename, nPosDot - 1) & "-resized.bmp"
Exit Function
ErrorHandler:
Err.Raise Err.Number, Err.Source, Err.Description
End Function
Private Sub ProcessPicture(ByRef the_sFilename As String, ByRef out_sProcessedFilename As String)
Dim sngPixelsX As Single
Dim sngPixelsY As Single
On Error GoTo ErrorHandler
' Get the size of our picture. Would have liked to have used a StdPicture object here, instead.
Set imgSize.Picture = LoadPicture(the_sFilename)
sngPixelsX = imgSize.Width
sngPixelsY = imgSize.Height
' If at least one of height and width is too bix, resize the biggest value down to m_ksngMaxPixels? and resize the other value proportionally.
If sngPixelsX > m_ksngMaxPixelsX Or sngPixelsY > m_ksngMaxPixelsY Then
If sngPixelsX > sngPixelsY Then
sngPixelsY = m_ksngMaxPixelsY * sngPixelsY / sngPixelsX
sngPixelsX = m_ksngMaxPixelsX
Else
sngPixelsX = m_ksngMaxPixelsX * sngPixelsX / sngPixelsY
sngPixelsY = m_ksngMaxPixelsY
End If
' Resize the canvas so that the persistent bitmap is the same size as the final picture, and then paint our picture onto that canvas, resizing down.
pbCanvas.Move 0!, 0!, sngPixelsX, sngPixelsY
pbCanvas.PaintPicture imgSize.Picture, 0!, 0!, sngPixelsX, sngPixelsY
' Get a reference to the persistent bitmap.
Set imgPic.Picture = pbCanvas.Image
out_sProcessedFilename = GetResizedFilename(the_sFilename)
SavePicture pbCanvas.Image, out_sProcessedFilename
Else
out_sProcessedFilename = the_sFilename
Set imgPic.Picture = imgSize.Picture
End If
Exit Sub
ErrorHandler:
Err.Raise Err.Number, Err.Source, Err.Description
End Sub
Private Sub SaveToDatabase(ByRef the_sProcessedFilename As String)
Dim nFileNo As Integer
Dim abytPictureFile() As Byte
Dim nFileLen As Long
' Open the file in binary mode, resize a byte array to fit the file's contents, load it into the array, and close the array.
nFileNo = FreeFile
Open the_sProcessedFilename For Binary As #nFileNo
nFileLen = LOF(nFileNo)
ReDim abytPictureFile(1 To nFileLen)
Get #nFileNo, , abytPictureFile()
Close #nFileNo
'
' YOUR WORK INSERTED HERE
'
Exit Sub
ErrorHandler:
Err.Raise Err.Number, Err.Source, Err.Description
End Sub
Private Sub UploadPicture(ByRef the_sFilename As String)
Dim sProcessedFilename As String
On Error GoTo ErrorHandler
ProcessPicture the_sFilename, sProcessedFilename
SaveToDatabase sProcessedFilename
Exit Sub
ErrorHandler:
Err.Raise Err.Number, Err.Source, Err.Description
End Sub
于 2012-08-02T07:15:29.737 回答
-1
// This will be your uploaded images folder
string target = Server.MapPath("~/ImageFolder" );
Image.GetThumbnailImageAbort thumbnailImageAbortDelegate =
new Image.GetThumbnailImageAbort(ThumbnailCallback);
foreach (UploadedFile file in RadUpload1.UploadedFiles)
{
file.SaveAs(Path.Combine(target, file.GetName()));
using (Bitmap originalImage = new Bitmap(file.InputStream))
{
// Set the resize here. You can use a constant or set a function here.
int width = 600;
int height = 600;
using (Image thumbnail = originalImage.GetThumbnailImage(width, height, thumbnailImageAbortDelegate, IntPtr.Zero))
{
string thumbnailFileName = Path.Combine(target,
string.Format("{0}_thumb{1}" , file.GetNameWithoutExtension(), file.GetExtension()));
thumbnail.Save(thumbnailFileName);
}
}
}
于 2012-08-02T06:29:18.277 回答