How to save an image instead of making a copy? - vb.net

I've been tasked with reducing the quality of pictures on a network drive's file/folder system.
Code:
Dim bmp1 As New Bitmap("U:\Image1.jpg")
Dim jpgEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Jpeg)
Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
Dim myEncoderParameters As New EncoderParameters(1)
Dim myEncoderParameter As New EncoderParameter(myEncoder, 50&)
myEncoderParameters.Param(0) = myEncoderParameter
bmp1.Save("U:\Image1.jpg", jpgEncoder, myEncoderParameters)
I get the following error: "A generic error occured in the GDI+".
Is there anyway to save instead of copying to another file?
Thanks in advance!

The first line of your code locks the file until bmp1 is disposed. That's why you can't save to the original path.
Here's the way I use to load an image from file without actually locking the file:
Dim bmp1 As Bitmap
Using bmpTmp As New Bitmap("U:\Image1.jpg")
bmp1 = New Bitmap(bmpTmp)
End Using
Using this you'll be able to save directly to the original file path:
'
'
bmp1.Save("U:\Image1.jpg", jpgEncoder, myEncoderParameters)
bmp1.Dispose()
Hope that helps :)

Related

VB.NET Display Image in PictureBox from Binary DB Value

I am trying to take a database binary entry where images are stored and convert it back to display in a PictureBox on a Windows Form. I have looked around and tried to piece a couple methods together however i get no errors and also no image in the PictureBox when trying to use the below code.
Dim MyByte = varReader("BinaryData")
Dim MyImg As Image
If MyByte IsNot Nothing Then
MyImg = bytesToImage(MyByte)
PictureBox1.Image = MyImg
End If
Public Function bytesToImage(ByVal byteArrayIn As Byte()) As Image
Dim ms As MemoryStream = New MemoryStream(byteArrayIn)
Dim returnImage As Image = Image.FromStream(ms)
Return returnImage
End Function
I know the Binary data is good as i have a different piece of code that has created it in the first place to store in the DB and the image displays fine on the Website which uses this data.
Any help or advice where i have gone wrong would be much appreciated!!
Many Thanks
EDITED::
The database column holding the information is varbinary(MAX)
This is the code i use to convert the image to the binary (I won't include the DB update part as this is just a basic update to the DB Column)
Dim varPictureBinary As Byte()
Dim filePath As String = "ImageFilePath"
Dim fStream As FileStream = New FileStream(filePath, FileMode.Open, FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fStream)
Dim fileInfo As FileInfo = New FileInfo(filePath)
varPictureBinary = br.ReadBytes(fileInfo.Length)
I then want to take a binary entry from this column and turn it back to an image and display it in a picture box in my program.

How to save real PictureBox

I need to save picturebox with real image as displayed.
Help me?
Ex:
Dim myEncoder As Encoder
Dim myImageCodecInfo As ImageCodecInfo
Dim myEncoderParameter As EncoderParameter
Dim myEncoderParameters As EncoderParameters
Dim bmp As Bitmap = CType(imgJpgPng.Image, Bitmap)
Dim bmpt As New Bitmap(640, 640)
Using g As Graphics = Graphics.FromImage(bmpt)
g.DrawImage(bmp, 0, 0, bmpt.Width, bmpt.Height)
End Using
myImageCodecInfo = GetEncoderInfo(ImageFormat.Jpeg)
myEncoder = Encoder.Quality
myEncoderParameters = New EncoderParameters(1)
myEncoderParameter = New EncoderParameter(myEncoder, CType(75L, Int32))
myEncoderParameters.Param(0) = myEncoderParameter
bmpt.Save("d:\ImgTemp\0000.JPG", myImageCodecInfo, myEncoderParameters)
From what I'm getting is that you want to save the picture box image, maybe to a directory or something. So, lets get right into this(It's very simple. By the way, I noticed you have Dim'd the bmpt at 640, 640, maybe that's a problem.
This is how I would save a picture box image
PictureBox1.Image.Save("C:\Users\Owner\Desktop\Webcam Application poctures\Picture.jpg")
Its very self explanatory, Picturebox1.Image.Save(saving the image) and in the quotations, there's the directory, and at the end there' a jpg, you can make it a .png, or any other file format.
If you want to have it being displayed as the original picture in the picture box, you will need to goto the properties of the picture box and make it bigger(size) or use the stretch feature.
Edit: I'm sorry I didn't read your question right, here's an update!
Read this website's information and look into it, this will help you greatly!
http://www.vb-helper.com/howto_resize_picture_save.html

Cannot write to a file after reading it with IO.StreamReader

I am making an encryption program that also functions as a notepad (CryptoMatic). There is a Open function with the following code:
Dim r As New StreamReader(filename)
Dim text As String = r.ReadToEnd
txtFileContents.text = text
r.Dispose()
r.Close()
Now there is the save and save as button
the save button checks if the file had been saved previously or not. If yes then I am trying to write to that file with the following code:
Dim myFileStream As New System.IO.FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.None)
myFileStream.Lock(0, 100)
Dim myWriter As New System.IO.StreamWriter(myFileStream)
myWriter.WriteLine(text)
myWriter.Flush()
myWriter.Close()
myFileStream.Close()
myFileStream.Unlock(0, 100)
Now when i press the save button it gives the following error:
The process cannot access the file 'C:\Users\VisualTech\Documents\Visual Studio 2010\Projects\CryptoMatic Update\CryptoMatic Update\bin\Debug\text.txt' because it is being used by another process.
and the application stops, can anyone please help me?
I have tried your code above and have noticed two things:
You don't need a myFileStream.Lock call because you already open it
with FileShare.None
You can't call myFileStream.Unlock(0, 100) after the call to
myFileStream.Close()
So I have rewritten your code in this way.
Sub Main
Dim text = "This is a text to write in the stream"
Using myFileStream As New System.IO.FileStream("D:\temp\testlock.txt", FileMode.Append, FileAccess.Write, FileShare.None)
Using myWriter As New System.IO.StreamWriter(myFileStream)
myWriter.WriteLine(text)
End Using
End Using
End Sub

how to load a file from folder to memory stream buffer

I am working on vb.net win form. My task is display the file names from a folder onto gridview control. when user clicks process button in my UI, all the file names present in gridview, the corresponding file has to be loaded onto memory stream buffer one after another and append the titles to the content of the file and save it in hard drive with _ed as a suffix to the file name.
I am very basic programmer. I have done the following attempt and succeeded in displaying filenames onto gridview. But no idea of later part. Any suggestions please?
'Displaying files from a folder onto a gridview
Dim inqueuePath As String = "C:\Users\Desktop\INQUEUE"
Dim fileInfo() As String
Dim rowint As Integer = 0
Dim name As String
Dim directoryInfo As New System.IO.DirectoryInfo(inqueuePath)
fileInfo = System.IO.Directory.GetFiles(inqueuePath)
With Gridview1
.Columns.Add("Column 0", "FileName")
.AutoResizeColumns()
End With
For Each name In fileInfo
Gridview1.Rows.Add()
Dim filename As String = System.IO.Path.GetFileName(name)
Gridview1.Item(0, rowint).Value = filename
rowint = rowint + 1
Next
Thank you very much for spending your valuable time to read this post.
to read a file into a memorystream is quite easy, just have a look at the following example and you should be able to convert it to suite your needs:
Dim bData As Byte()
Dim br As BinaryReader = New BinaryReader(System.IO.File.OpenRead(Path))
bData = br.ReadBytes(br.BaseStream.Length)
Dim ms As MemoryStream = New MemoryStream(bData, 0, bData.Length)
ms.Write(bData, 0, bData.Length)
then just use the MemoryStream ms as you please. Just to clearify Path holds the full path and filename you want to read into your memorystream.

How to write string data to tiff file in vb.net

I need to write some data which is string to tiff file. I am doing in the following way
Dim OFile As System.IO.File
Dim OWrite As system.IO.TextWriter
OWrite = OFile.CreateText("Signature.tiff")
OWrite.Write(ControlData)
MessageBox.Show("Signature is recieved and it is saved in Signature.tiff")
ControlData is the string which is to be written to the file.
I am capturing the signature from the user. This function gets the data in string format and i need to create a tiff file using the string data.
When i did in this way, signature.tiff is created but when i opened the image it is giving no preview available.
Can you tell me what is the problem or correct way of doing this?
Thanks a lot.
Dim format As StringFormat = New StringFormat()
Dim MyRect As Rectangle = New Rectangle(0, 0, 400, 400)
Dim MyGraphics As Graphics = Me.CreateGraphics()
Dim MyImg As Image = New Bitmap(MyRect.Width, MyRect.Height, MyGraphics)
Dim imageGraphics As Graphics = Graphics.FromImage(MyImg)
imageGraphics.FillRectangle(Brushes.White, MyRect)
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center
imageGraphics.DrawString("Hello Everyone", objFont, Brushes.Black, RectangleF.op_Implicit(MyRect))
MyGraphics.DrawImage(MyImg, MyRect)
MyImg.Save(filename)
Just see this may help you all for converting text string to image.
Thanks.
TIFF files are binary image files. You are writing the string to a text file. Try opening the file in Notepad to check.
You need a way to create an image in memory and save it to TIFF format.
You could use a PictureBox control. Write the string onto the PictureBox then save as a TIFF.