[英]Resize an user upload photo to fit my picture box in VB 6.0
我正在使用VB 6.0開發一個庫存管理項目。 現在,我想包含一個功能,該功能允許用戶將其商品的照片上傳到我的系統。 然后我想將其保存到數據庫中。 此外,我想將照片尺寸限制為600 * 600。 因此,當用戶上傳大於600 * 600 pix的圖片時,我的系統應自動調整照片尺寸以適合我的圖片框。 有人可以幫忙嗎? 提前致謝。
沒有簡單的方法可以做到這一點。 我將利用VB標准控件的副作用。 采取表單/ UserControl /任何形式,並粘貼以下控件:
我添加了<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
// 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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.