Call embeded font into label - vb.net

Hey all i am trying to call my embedded font AbrahamLincoln into my label although when i run the program it never changes the font...
Private Sub slackerR_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim sMyFonts As String() = {"AbrahamLincoln.ttf"}
Dim fEmbedded As New Font(GetFont(sMyFonts).Families(0), 10)
Label1.Font = fEmbedded
End Sub
Public Function GetFont(ByVal FontResource() As String) As Drawing.Text.PrivateFontCollection
'Get the namespace of the application
Dim NameSpc As String = Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString()
Dim FntStrm As IO.Stream
Dim FntFC As New Drawing.Text.PrivateFontCollection()
Dim i As Integer
For i = 0 To FontResource.GetUpperBound(0)
'Get the resource stream area where the font is located
FntStrm = Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(NameSpc + "." + FontResource(i))
'Load the font off the stream into a byte array
Dim ByteStrm(CType(FntStrm.Length, Integer)) As Byte
FntStrm.Read(ByteStrm, 0, Int(CType(FntStrm.Length, Integer)))
'Allocate some memory on the global heap
Dim FntPtr As IntPtr = Runtime.InteropServices.Marshal.AllocHGlobal(Runtime.InteropServices.Marshal.SizeOf(GetType(Byte)) * ByteStrm.Length)
'Copy the byte array holding the font into the allocated memory.
Runtime.InteropServices.Marshal.Copy(ByteStrm, 0, FntPtr, ByteStrm.Length)
'Add the font to the PrivateFontCollection
FntFC.AddMemoryFont(FntPtr, ByteStrm.Length)
'Free the memory
Runtime.InteropServices.Marshal.FreeHGlobal(FntPtr)
Next
Return FntFC
End Function
I've tried both {"AbrahamLincoln.ttf"} and {"AbrahamLincoln"} and both do not work.
Using VB.net 2010.

This may be an easier way for you...
Put font in your resources.
Add a module like this: (change the resource names below "My.Resources.[your resource name]")
Module agencyFontNormal
Private _pfc As PrivateFontCollection = Nothing
Public ReadOnly Property GetInstance(ByVal Size As Single, ByVal style As FontStyle) As Font
Get
If _pfc Is Nothing Then LoadFont()
Return New Font(_pfc.Families(0), Size, style)
End Get
End Property
Private Sub LoadFont()
Try
_pfc = New PrivateFontCollection
Dim fontMemPointer As IntPtr = Marshal.AllocCoTaskMem(My.Resources.AGENCYNORMAL.Length)
Marshal.Copy(My.Resources.AGENCYNORMAL, 0, fontMemPointer, My.Resources.AGENCYNORMAL.Length)
_pfc.AddMemoryFont(fontMemPointer, My.Resources.AGENCYNORMAL.Length)
Marshal.FreeCoTaskMem(fontMemPointer)
Catch ex As Exception
End Try
End Sub
End Module
Call via:
Dim ff As Font = agencyFontNormal.GetInstance(12, FontStyle.Regular)

Related

Utilizing wildcards and variables for getFiles

I am kinda new to VB.net, so I am not sure if I try this the right way. I have the following piece of code.
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Dim TextLine As String
Do While objReader.Peek() <> -1
Dim newString As String = TextLine.Replace(vbCr, "").Replace(vbLf, "") & ".wav"
Dim SongName As String = My.Computer.FileSystem.GetName(newString)
Dim MyFile As String = Dir("C:\AllSongs\" & newString)
Dim Searchquery As IEnumerable(Of String) = IO.Directory.EnumerateFiles("C:\AllSongs", "*", IO.SearchOption.AllDirectories).Where(Function(f) IO.Path.GetFileNameWithoutExtension(f).IndexOf(SongName, StringComparison.CurrentCultureIgnoreCase) >= 0)
For Each Result In Searchquery
ListBox1.Items.Add(Result)
Next
I am trying to use the lines in the text file, and get the .wav in AllSongs dir that partially correspond in these files. Can it be done?
Edit: Part of the code contains a media player. I want to be able to play songs from this player, by choosing files in the list.
Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
AxWindowsMediaPlayer1.URL = ListBox1.SelectedItem
Dim variables As New Dictionary(Of String, String)()
Dim selectedItem As Object = ListBox1.SelectedItem
variables("MyDynamicVariable") = selectedItem ' Set the value of the "variable"
selectedItem1 = selectedItem
Dim value As String = variables("MyDynamicVariable") ' Retrieve the value of the variable
End Sub
Incidental to the question, but important, is that when you're working with files it's often necessary to clean up some resources (file handles, I guess) that the operating system uses even though you don't see it directly in the code as written. There is a way of doing that automatically with the Using statement, as shown in the following code.
To find out if a filename contains some text (string), you can extract the filename with no path or extension with Path.GetFileNameWithoutExtension and check if it contains the desired string with the IndexOf function, which lets you ignore uppercase/lowercase by specifying how to do the check.
I notice from an update to the question that some improvements can be made, such as using a Class for the song entries so that the displayed list can have more information behind it, which means that the song name can be shown on its own but you can get the full path to the file when you click on it.
I made a new Windows Forms project and added just a ListBox to it, and used this code to show the full path (which you can use for your media player) to the song when its name is double-clicked:
Imports System.IO
Public Class Form1
Public Class SongEntry
Property Name As String
Property FullName As String
End Class
Sub PopulateSongList(musicDirectory As String, songsList As String)
Dim songList As New List(Of SongEntry)
Using sr As New System.IO.StreamReader(songsList)
Do While Not sr.EndOfStream
Dim thisSong = sr.ReadLine()
If thisSong <> "NaN" Then
Dim fs = Directory.EnumerateFiles(musicDirectory, "*.wav", SearchOption.AllDirectories).
Where(Function(f) Path.GetFileNameWithoutExtension(f).IndexOf(thisSong, StringComparison.CurrentCultureIgnoreCase) >= 0).
Select(Function(g) New SongEntry With {.Name = Path.GetFileNameWithoutExtension(g), .FullName = g})
songList.AddRange(fs)
End If
Loop
End Using
ListBox1.DataSource = songList
ListBox1.DisplayMember = "Name"
ListBox1.ValueMember = "FullName"
End Sub
Private Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick
Dim lb = DirectCast(sender, ListBox)
If lb.SelectedIndex >= 0 Then
Dim fullPathToSong = lb.SelectedValue.ToString()
MsgBox(fullPathToSong)
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim songsList = "C:\temp\AllSongs\songsList.txt"
Dim musicDirectory = "C:\temp\AllSongs"
PopulateSongList(musicDirectory, songsList)
End Sub
End Class

VB.NET Arithmetic operation resulted in an overflow using FileStream

I'm trying to copy files from a directory to another using the FileStream and it works fine, but when copying large files it throws me an error Arithmetic operation resulted in an overflow. It cannot copy a file larger than 20MB. Please I'm new to programming can you help me with this?
Private mCurrentFile As String = String.Empty
Public ReadOnly Property CurrentFile() As String
Get
Return mCurrentFile
End Get
End Property
Dim FS As FileStream
mCurrentFile = GetFileName(URL)
Dim wRemote As WebRequest
Dim bBuffer As Byte()
ReDim bBuffer(256)
Dim iBytesRead As Integer
Dim iTotalBytesRead As Integer
FS = New FileStream(Location, FileMode.Create, FileAccess.Write)
wRemote = WebRequest.Create(URL)
Dim myWebResponse As WebResponse = wRemote.GetResponse
RaiseEvent FileDownloadSizeObtained(myWebResponse.ContentLength)
Dim sChunks As Stream = myWebResponse.GetResponseStream
Do
iBytesRead = sChunks.Read(bBuffer, 0, 256)
FS.Write(bBuffer, 0, iBytesRead)
iTotalBytesRead += iBytesRead
If myWebResponse.ContentLength < iTotalBytesRead Then
RaiseEvent AmountDownloadedChanged(myWebResponse.ContentLength)
Else
RaiseEvent AmountDownloadedChanged(iTotalBytesRead)
End If
Loop While Not iBytesRead = 0 And dStop = False
sChunks.Close()
FS.Close()
Private Sub _Downloader_FileDownloadSizeObtained(ByVal iFileSize As Long) Handles _Downloader.FileDownloadSizeObtained
'FIRES WHEN WE HAVE GOTTEN THE DOWNLOAD SIZE, SO WE KNOW WHAT BOUNDS TO GIVE THE PROGRESS BAR
ProgressBar1.Value = 0
ProgressBar1.Maximum = CInt(iFileSize)
End Sub
The error throws here.
Private Sub _Downloader_AmountDownloadedChanged(ByVal iNewProgress As Long) Handles _Downloader.AmountDownloadedChanged
'FIRES WHEN MORE OF THE FILE HAS BEEN DOWNLOADED
ProgressBar1.Value = Convert.ToInt64(iNewProgress) '<-------------- Error throws here
Application.DoEvents()
End Sub
I don't know any more larger type that can handle large files or is there any other way?

print images from a folder in vb .net with 8 images per page

i select a folder using vb .net.my code is like below--
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If (FolderBrowserDialog1.ShowDialog() = DialogResult.OK) Then
TextBox1.Text = FolderBrowserDialog1.SelectedPath
End If
End Sub
But the problem is i don't have any idea about displaying the images(8 images Per page)
VB.Net Version:
Since you want to create print preview, I would suggest you to create a new Form just for the print preview and track the PageNumber which the user inputs
Dim pageNumber As Integer = 0 'start from 0, but change this according to the user input accordingly
You could get the list of files from the given folder by using Directory.GetFiles in the System.IO
Dim rawpaths As List(Of String) = Directory.GetFiles(folder).ToList() 'This gets all files, not only images
You may need to provide a list of acceptable image extension according to this.
Dim validImageFormat As New List(Of String) From {"jpg", "bmp", "png", "jpeg", "gif", "tiff"}
And filter your file results such that they only contains acceptable image results like this (using Split method, Contains, etc...)
Dim paths As List(Of String) = New List(Of String)
For Each rawpath As String In rawpaths
Dim rawwords As String() = rawpath.Split(".") 'split the rawpath, the important is the last element, which is the file extension
If (validImageFormat.Contains(rawwords(rawwords.Length - 1))) Then 'the rawpath is a valid image path
paths.Add(rawpath) 'this is a valid image path
End If
Next
Then, in your print preview Form you may need to list your 8 ListBoxes to ease you to control the display later as well as to handle the case where the number of the images are not multiplication of 8
Dim pbList As List(Of PictureBox) = New List(Of PictureBox)
Private Sub printPreviewForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
pbList.Add(PictureBox1) 'list your picture box will help you later
pbList.Add(PictureBox2)
pbList.Add(PictureBox3)
pbList.Add(PictureBox4)
pbList.Add(PictureBox5)
pbList.Add(PictureBox6)
pbList.Add(PictureBox7)
pbList.Add(PictureBox8)
End Sub
Then, you need to determine, how many pictures are to be shown (min of 1, max of 8, based on the PageNumber)
If (pageNumber * 8 > paths.Count) Then 'exceeds the possible image display
Return 'this is not allowed, do something
End If
Dim minPathNo As Integer = pageNumber * 8 'get the min and max for later display
Dim maxPathNo As Integer = Math.Min(pageNumber * 8 + 7, paths.Count - 1)
Finally, to display, you can use Image.FromFile() method to load the image file from your folder plus using the minPathNo and maxPathNo declared before to show the images safely
If (pageNumber * 8 > paths.Count) Then 'exceeds the possible image display
Return 'this is not allowed, do something
End If
Dim minPathNo As Integer = pageNumber * 8
Dim maxPathNo As Integer = Math.Min(pageNumber * 8 + 7, paths.Count - 1)
For i As Integer = minPathNo To minPathNo + 7
Dim pbIndex As Integer = i - minPathNo
If i <= maxPathNo Then
pbList(pbIndex).Image = Image.FromFile(paths(i)) 'display existing image
Else
pbList(pbIndex).Image = Nothing 'don't display non-existing image
End If
Next
Edit:
Suppose your form looks like this:
Then this is how your code would look like in just one form. In your case, you have to make two forms:
Imports System.IO
Public Class Form1
Dim folder As String = "C:\MyPics"
Dim validImageFormat As New List(Of String) From {"jpg", "bmp", "png", "jpeg", "gif", "tiff"}
Dim pbList As List(Of PictureBox) = New List(Of PictureBox)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rawpaths As List(Of String) = Directory.GetFiles(folder).ToList() 'Assuming all the file is image
Dim paths As List(Of String) = New List(Of String)
For Each rawpath As String In rawpaths
Dim rawwords As String() = rawpath.Split(".") 'split the rawpath, the important is the last element, which is the file extension
If (validImageFormat.Contains(rawwords(rawwords.Length - 1))) Then 'the rawpath is a valid image path
paths.Add(rawpath) 'this is a valid image path
End If
Next
Dim pageNumber As Integer = NumericUpDown1.Value
If (pageNumber * 8 > paths.Count) Then 'exceeds the possible image display
Return 'this is not allowed, do something
End If
Dim minPathNo As Integer = pageNumber * 8
Dim maxPathNo As Integer = Math.Min(pageNumber * 8 + 7, paths.Count - 1)
For i As Integer = minPathNo To maxPathNo
Dim currentPictureBox As Integer = i - minPathNo
pbList(currentPictureBox).Image = Image.FromFile(paths(i))
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
pbList.Add(PictureBox1)
pbList.Add(PictureBox2)
pbList.Add(PictureBox3)
pbList.Add(PictureBox4)
pbList.Add(PictureBox5)
pbList.Add(PictureBox6)
pbList.Add(PictureBox7)
pbList.Add(PictureBox8)
End Sub
End Class
And you only need to change the numericUpDown (simulating your page` to 0, 1, 2, etc...)

how to read from text file to textbox in visual basic 1 line every hit on button

I have files type .txt (Text File) and it's have multiple line and i have these pictures
i want to make program generate fake information
Mean: when some one hit generate button it's ready from text file and fill textbox in visual basic
every hit(press) on Generate Button make program generate new information from text files (.txt)
i tried a lot ways:
Code:
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\test.txt")
Code:
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\test.txt", _
System.Text.Encoding.UTF32)
and this
Code:
Dim oFile as System****.File
Dim oRead as System****.StreamReader
oRead = oFile.OpenText(“C:\test.txt”)
and this
Code:
Dim FILE_NAME As String = "C:\Users\user\Desktop\test.txt"
Dim objReader As New System.I--O.StreamReader(FILE_NAME)
TextBox1.Text = objReader.ReadToEnd
Code:
' Form Load :
Dim text As String = MsgBox("text you want to make the form remember it.")
Or new Sub :
Code:
Private Sub Text
text
Code:
' Button Click :
Text()
Code:
Dim path As String = "THE FILE PATH" 'The file path
Dim reader As New IO.StreamReader(path)
Dim lineIndex As Integer = 2 ' The index of the line that you want to read
For i As Integer = 0 To lineIndex - 1
reader.ReadLine()
Next
TextBox1.Text = reader.ReadLine
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = ReadLineFromTxt("THE TXT FILE PATH", 0) '0 is the line index
End Sub
Public Shared Function ReadLineFromTxt(ByVal path As String, ByVal lineIndex As Integer) As String
Dim reader As New IO.StreamReader(path)
For I As Integer = 0 To lineIndex - 1
reader.ReadLine()
Next
Return reader.ReadLine()
End Function
End Class
These ways take from members in this fourm:
http://www.mpgh.net/forum/33-visual-basic-programming/693165-help-how-can-i-generate-text-txt-file-textbox-when-button-click-2.html
if are these ways working please tell me how to use it in best way
i have Visual studio 2012 and updated 1
With all due respect
Assuming you are reading from a file and displaying lines on the form, you can use these.
If you have a large file (> 10MB), then you can use this pattern... (syntax from memory, please excuse mistype)
Public Class YourFormNameHere
Private _CurrentLine as Integer = 0
Private Sub btnClicked(sender, e) 'or enter pressed - This is YOUR keypress event handler.
Using Dim sr as New StreamReader(filePath)
Dim _curIndex as Integer = 0
While (sr.EndOfFile == false)
Dim _line as String = sr.ReadLine()
If (_curIndex = _CurrentLine)
txtLineDisplay.Text = _line
Break
End If
curIndex += 1
End While
End Using
End Sub
End Class
If you have a smaller file, then use this pattern.
Public Class YourFormNameHere
Private _Lines as String()
Private _CurrentLine as Integer = 0
Private Sub formLoad(sender, e) 'one-time load event - This is YOUR form load event
_Lines = File.ReadAllLines(filePath)
End Sub
Private Sub btnClicked(sender, e) 'or enter pressed - This is YOUR keypress event handler.
txtLineDisplay.Text = _Lines(_CurrentLine)
_CurrentLine += 1
End Sub
End Class

how to get the thumbnail of picture in windows mobile application

I am delveloping a small application in VB.NET, where I need to load an image from the phone into a picture box. I am not able to add the picture into picturebox, though. It throws an OutOfMemoryException. I am wondering if there is any way to reduce the size of the picture by changing it to thumbnail.
Dim srcmap As New Bitmap(OpenFileDialog1.FileName)
Dim destbit As New Bitmap(220, 220)
Dim srcRec As New Rectangle(0, 0, srcmap.Width, srcmap.Height)
Dim destRec As New Rectangle(0, 0, 220, 220)
Dim g As Graphics
g = Graphics.FromImage(destbit)
g.DrawImage(srcmap, destRec,srcRec, GraphicsUnit.Pixel)
picturebox.Image = destbit
Here is my OpenNetCF C# snippet for that issue:
...
//imagefactory
using OpenNETCF.Drawing;
using OpenNETCF.Drawing.Imaging;
...
OpenNETCF.Drawing.Imaging.StreamOnFile m_stream;
Size m_size;
///
/// this will handle also large bitmaps and show a thumbnailed version on a picturebox
/// see http://blog.opennetcf.com/ctacke/2010/10/13/LoadingPartsOfLargeImagesInTheCompactFramework.aspx
///
/// the name of the file to load
private void showImage(string sFileName)
{
var stream = File.Open(sFileName, FileMode.Open);
m_stream = new StreamOnFile(stream);
m_size = ImageHelper.GetRawImageSize(m_stream);
System.Diagnostics.Debug.WriteLine("showImage loading " + sFileName + ", width/height = " + m_size.Width.ToString() + "/"+ m_size.Height.ToString());
//CameraPreview.Image = ImageHelper.CreateThumbnail(m_stream, CameraPreview.Width, CameraPreview.Height);
CameraSnapshot.Image = ImageHelper.CreateThumbnail(m_stream, CameraPreview.Width, CameraPreview.Height);
showSnapshot(true); //show still image
m_stream.Dispose();
stream.Close();
}
with imagehelper.cs:http://code.google.com/p/intermeccontrols/source/browse/DPAG7/Hasci.TestApp.And_Controls/IntermecControls/Hasci.TestApp.IntermecCamera3/ImageHelper.cs
Here is the VB code of the above:
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports OpenNETCF.Drawing
Imports OpenNETCF.Drawing.Imaging
Imports System.Runtime.InteropServices
Imports System.Resources
Imports System.Reflection
Public Class ImageHelper
Private Shared m_factory As ImagingFactory
Private Shared Function GetFactory() As ImagingFactory
If (m_factory Is Nothing) Then
m_factory = New ImagingFactory
End If
Return m_factory
End Function
Public Shared Function CreateClip(ByVal sof As StreamOnFile, ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer) As Bitmap
Dim original As IBitmapImage = Nothing
Dim image As IImage = Nothing
Dim info As ImageInfo
GetFactory.CreateImageFromStream(sof, image)
Try
image.GetImageInfo(info)
GetFactory.CreateBitmapFromImage(image, info.Width, info.Height, info.PixelFormat, InterpolationHint.InterpolationHintDefault, original)
Try
Dim ops As IBasicBitmapOps = CType(original, IBasicBitmapOps)
Dim clip As IBitmapImage = Nothing
Try
Dim rect As RECT = New RECT(x, y, (x + width), (y + height))
ops.Clone(rect, clip, True)
Return ImageUtils.IBitmapImageToBitmap(clip)
Finally
Marshal.ReleaseComObject(clip)
End Try
Finally
Marshal.ReleaseComObject(original)
End Try
Finally
Marshal.ReleaseComObject(image)
End Try
End Function
Public Shared Function getScaledBitmap(ByVal sof As StreamOnFile, ByVal scalePercent As Integer) As Bitmap
Dim thumbnail As IBitmapImage = Nothing
Dim image As IImage = Nothing
Dim info As ImageInfo
Dim fScale As Decimal = (scalePercent / 100)
' m()
' do not remove the m specifier!
GetFactory.CreateImageFromStream(sof, image)
Try
image.GetImageInfo(info)
Dim newWidth As UInteger = CType((info.Width * fScale), UInteger)
Dim newHeight As UInteger = CType((info.Height * fScale), UInteger)
GetFactory.CreateBitmapFromImage(image, newWidth, newHeight, info.PixelFormat, InterpolationHint.InterpolationHintDefault, thumbnail)
Try
Return ImageUtils.IBitmapImageToBitmap(thumbnail)
Catch ex As Exception
Dim stream As System.IO.Stream = System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream("CameraCx7x.oom.png")
Return New Bitmap(stream)
' (400, 300);
Finally
Marshal.ReleaseComObject(thumbnail)
End Try
Finally
Marshal.ReleaseComObject(image)
End Try
End Function
Public Shared Function CreateThumbnail(ByVal sof As StreamOnFile, ByVal width As Integer, ByVal height As Integer) As Bitmap
Dim thumbnail As IBitmapImage = Nothing
Dim image As IImage = Nothing
Dim info As ImageInfo
GetFactory.CreateImageFromStream(sof, image)
Try
image.GetImageInfo(info)
GetFactory.CreateBitmapFromImage(image, CType(width, UInteger), CType(height, UInteger), info.PixelFormat, InterpolationHint.InterpolationHintDefault, thumbnail)
Try
Return ImageUtils.IBitmapImageToBitmap(thumbnail)
Finally
Marshal.ReleaseComObject(thumbnail)
End Try
Finally
Marshal.ReleaseComObject(image)
End Try
End Function
Public Shared Function saveScaledBitmap(ByVal sof As StreamOnFile, ByVal width As Integer, ByVal height As Integer, ByVal sNewFile As String) As Boolean
Dim bRet As Boolean = True
Try
Dim bmp As Bitmap = CreateThumbnail(sof, width, height)
bmp.Save(sNewFile, System.Drawing.Imaging.ImageFormat.Jpeg)
Catch ex As Exception
System.Diagnostics.Debug.WriteLine(("Exception in saveScaledBitmap(): " + ex.Message))
bRet = False
End Try
Return bRet
End Function
Public Shared Function GetRawImageSize(ByVal sof As StreamOnFile) As Size
Dim image As IImage = Nothing
Dim info As ImageInfo
GetFactory.CreateImageFromStream(sof, image)
Try
image.GetImageInfo(info)
Return New Size(CType(info.Width, Integer), CType(info.Height, Integer))
Finally
Marshal.ReleaseComObject(image)
End Try
End Function
End Class
and this is how to call it from a form with a lable, a button and a picturebox
Imports OpenNETCF.Drawing
Imports OpenNETCF.Drawing.Imaging
Imports System.IO
Public Class Form1
Dim _Bitmap As Bitmap
Private Sub showImage(ByVal filePath As String)
'do not load the full image!
If File.Exists(filePath) Then
label1.Text = filePath
If (Not (_Bitmap) Is Nothing) Then
_Bitmap.Dispose()
End If
' a 1944x2593 image is about 15MByte !
'create a scaled down image file
Dim _filestream As FileStream = File.Open(filePath, FileMode.Open)
Dim _stream As StreamOnFile = New StreamOnFile(_filestream)
Dim fi As System.IO.FileInfo = New FileInfo(filePath)
Dim sizeBmp As Size = ImageHelper.GetRawImageSize(_stream)
'allow 640 hight
Dim ratio As Integer = (sizeBmp.Height / 640)
Dim scale As Integer = (ratio * 10)
If (ratio > 1) Then
_Bitmap = ImageHelper.getScaledBitmap(_stream, scale)
End If
Me.pictureBox1.Image = CType(_Bitmap, Image)
_filestream.Close()
End If
End Sub
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
Dim ofd As New OpenFileDialog
ofd.InitialDirectory = "\My Documents"
ofd.Filter = "Jpg image|*.jpg|Bmp image|*.bmp|Gif image|*.gif|All files|*.*"
ofd.FilterIndex = 0
Dim sFile As String
If (ofd.ShowDialog = Windows.Forms.DialogResult.OK) Then
sFile = ofd.FileName
ofd.Dispose()
showImage(sFile)
End If
End Sub
End Class