Visual Basic Reading Saving Images Into Access - vb.net

Can someone please give me an explanation to this code, and for your information I am trying to save a picture which is displayed in a picture box and save it to the Microsoft access database, I don't understand what anything means, and especially the 0.
If Not PictureBox1.Image Is Nothing Then
Dim fsreader As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
Dim breader As New BinaryReader(fsreader)
Dim imgbuffer(fsreader.Length) As Byte
breader.Read(imgbuffer, 0, fsreader.Length)
fsreader.Close()

Your snippet code is code to save a binary of picture to imgbuffer variable in VB.Net. This snippet code didn't save this images into Ms Access. But...
I will try to explain it, how this code works :
If Not PictureBox1.Image Is Nothing Then 'This code for checking if there's any images in the picture box, if it's so run next code.
Dim fsreader As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read) 'This variable declared for getting file name, access file and the read mode. And open it using `Filestream` variable, so after it's opened, the length of images will be saved into `fsreader` variable
Dim breader As New BinaryReader(fsreader) 'This code for reading the binary of images from `fsreader` variable and put the image buffer from range of byte to a variable
Dim imgbuffer(fsreader.Length) As Byte 'This variable is use to collect image buffer with specified length of binary images from `fsreader` variable.
breader.Read(imgbuffer, 0, fsreader.Length) 'This code is use for reading binary of images with specified length of byte and put the image buffer into a imgbuffer variable. The number `0` in the brackets means the binary images will be put fully in imgbuffer. Why `0` ? can I use larger than `0` ? yes you can but the image will be corrupt or differ from the original, so you must read the binary images from 0 to the image file size.
fsreader.Close() 'This is for closing `fsreader` from accessing images file.
So, what's next ?
From that snippet code, you can continued it to process the imgbuffer variable to save it into Ms-Access using some library collection that provided in VB.Net, like OleDB or anything else. At last, I would prefer using MemoryStream to put image buffer and save it into Bitmap variable.
Hope this explain your snippet code.

Related

iText7 Merge of 2 PDF MemorStreams Not Working

I am upgrading some older iTextSharp code to the new iText 7 libraries. I am having a lot of trouble determining the proper way to merge 2 PDF MemoryStreams into one PDF MemoryStream that contains all the pages from both source PDF MemoryStreams. It seems simple and I think the code below is set up properly but the resulting PDF memory stream only contains the first file. The second PDF file is never present and never concatenated to the first.
I have found multiple ways documented on the Internet as the "proper" way to do the merge. The actual sample code with iText 7 seems to be unusually complex (in that is mixes multiple concepts into one sample repeatedly - as in doesn't reduce the concept to the simplest possible code), and seems to fail to demonstrate simple concepts. For instance, their PDFMerge documentation has no sample code at all in the documentation (nor does anything else I looked at in the class documentation). The examples they have online actually always mix merging from files (not MemoryStreams) with other concepts like adding page numbers or adding Table of Contents. So they never just show one concept and they never start with anything other than files. My PDFs are coming out of a database and we just need to merge them into one PDF memory stream and save it back out. My concern is that maybe I am not creating the MemoryStream properly when I initialize the PDFWriter. As none of their samples ever do anything but initial with an actual file, I was unable to confirm this was done properly. I also fully qualified all objects in the code because I want to leave the old iTextSharp code in place while I am upgrading to the new iText 7. This was done to make sure an iTextSharp object of the same name wasn't inadvertently being unknowingly used.
Also, in the interest of making the source as easy as possible to read I removed some of the declarations and initialization of objects being used. Everything was traced through and all values are fully loaded with proper values as you trace through the code. The only problem is that the second PDFMerge doesn't seem to do anything. I am assuming the problem is that I didn't prepare the PDF objects properly or that I have to do something special with the PDFWriter on the Destination PDF Document (p_pdfDocument) before the second PDF is written out with the PDFMerge object.
Dim p_bResult As Boolean = False
Dim p_bArray As Byte() = Nothing
Dim p_memStream As New System.IO.MemoryStream
Dim p_pdfWriter As New iText.Kernel.Pdf.PdfWriter(p_memStream)
Dim p_pdfDocument As New iText.Kernel.Pdf.PdfDocument(p_pdfWriter)
Dim p_pdf1Stream As New System.IO.MemoryStream(CType(p_cImage1.ImageFile, Byte()))
Dim p_pdf2Stream As New System.IO.MemoryStream(CType(p_cImage2.ImageFile, Byte()))
Dim p_pdf1Reader As New iText.Kernel.Pdf.PdfReader(p_pdf1Stream)
Dim p_pdf2Reader As New iText.Kernel.Pdf.PdfReader(p_pdf2Stream)
Dim p_pdf1Document As New iText.Kernel.Pdf.PdfDocument(p_pdf1Reader)
Dim p_pdf2Document As New iText.Kernel.Pdf.PdfDocument(p_pdf2Reader)
Dim p_pdfMerger As New iText.Kernel.Utils.PdfMerger(p_pdfDocument)
p_pdfMerger.Merge(p_pdf1Document, 1, p_pdf1Document.GetNumberOfPages())
p_pdfMerger.Merge(p_pdf2Document, 1, p_pdf2Document.GetNumberOfPages())
'Problem is here... the array only has the first PDF in it
'The second p_pdfMerger.Merge didn't seem to do anything
p_bArray = p_memStream.ToArray
p_pdf1Document.Close()
p_pdf2Document.Close()
p_pdfDocument.Close()
I expected the 2 source PDF MemoryStreams to be present in the destination MemoryStream but it only contains the first PDF in it.
Edit:
I changed the ending to...
p_pdfMerger.Merge(p_pdf1Document, 1, p_pdf1Document.GetNumberOfPages())
p_pdfMerger.Merge(p_pdf2Document, 1, p_pdf2Document.GetNumberOfPages())
p_cImage1.PageCount = p_pdfDocument.GetNumberOfPages()
p_pdfDocument.Close()
p_bArray = p_memStream.ToArray
p_pdf1Document.Close()
p_pdf2Document.Close()
Thing is that the p_pdfDocument.GetNumberOfPages() is correct but bytes are still just first PDF document when saved to database and viewed.
I tested your use case, condensing your code a bit, reading the input memory streams from files, and writing the output memory stream to a file as I don't have your database environment:
Using MemoryStream As New MemoryStream,
Pdf1MemoryStream As New MemoryStream(File.ReadAllBytes(MY_FIRST_PDF_FILE)),
Pdf2MemoryStream As New MemoryStream(File.ReadAllBytes(MY_SECOND_PDF_FILE))
Using PdfDocument As New PdfDocument(New PdfWriter(MemoryStream)),
Pdf1 As New PdfDocument(New PdfReader(Pdf1MemoryStream)),
Pdf2 As New PdfDocument(New PdfReader(Pdf2MemoryStream))
Dim Merger As New PdfMerger(PdfDocument)
Merger.Merge(Pdf1, 1, Pdf1.GetNumberOfPages)
Merger.Merge(Pdf2, 1, Pdf2.GetNumberOfPages)
End Using
Dim PdfBytes As Byte() = MemoryStream.ToArray()
Using FileStream As Stream = File.Create("TwoPdfsMergedInMemoryStream.pdf")
FileStream.Write(PdfBytes, 0, PdfBytes.Length)
End Using
End Using
As result I got the contents of both source files in TwoPdfsMergedInMemoryStream.pdf as it should be. Concerning your observation
Thing is that the p_pdfDocument.GetNumberOfPages() is correct but bytes are still just first PDF document when saved to database and viewed.
therefore, I would assume that p_bArray does contain a PDF with the contents of both your source PDFs but that there is an issue in saving to database or viewing.
To test this you could save the contents of the byte array to a file somewhere like I do above; then you can check what really is in the array.

System.IO.StreamReader returns corrupted output

The below given code is returning corrupted data to the variable 'mystr'
like
PK ! ��fѲ �  [Content_Types].xml �(� �UMk�#���^��N%�9�ɱ
It was reading the word files correctly. Suddenly it started happening without any change in code or software versions or source files!! Any word file I try to run through the below code gives the same corrupted output. I'm able to open the file in MsWord without any issue.
Dim myStreamReader As System.IO.StreamReader
Dim myStr As String
myStreamReader = System.IO.File.OpenText("c:\test.docx")
myStr = myStreamReader.ReadToEnd()
myStreamReader.Close()
Any suggestions why it is happening ?
You can't read a Word document using the StreamReader class as the file itself isn't plain text (it contains the data you find 'corrupted').
This link will help you reading text from a Word document.

Issues with VB and Aspose

I have the following lines of code:
Dim ms As New MemoryStream(my_memory_stream)
workbook As New Workbook(ms)
workbook.Save("C:\book1.xlsx")
My purpose is to save the stream contained in my_memory_stream into an xlsx file named "book1": the problem is that when I run this code an exception occurr (Invalid Excel2007Xlsx file format).
Does anyone know what I'm doing wrong?
Thanks so much!
Most probably, the problem is the base64 format. It is in the form of ASCII characters. You need to convert the Base64 data to binary. And then load the binary data in Workbook class.
' Decode base64
Dim binaryBytes As Byte() = Convert.FromBase64CharArray(base64Data, 0, base64Data.Length)
' Load in MemoryStream
Dim binaryStream As New MemoryStream(binaryBytes)
' Pass memory stream to Workbook
Dim workbook As New Aspose.Cells.Workbook(my_memory_stream)
workbook.Save(dataDir + "workbook-out.xlsx")
Also note that, if your decoding is correct, you can directly save the binary byte or stream to disk and verify that the Excel is saved correctly.
PS. I am a Developer Evangelist at Aspose.
It seems that you are trying to save a stream directly to a Excel file. Excel files have a fixed format, and the stream will not be adhering to the standard file protocol of excel.
I think you would like to use Workbook object of Microsoft.office.Interop.Excel namespace.

Openpop.NET retriving attachment to specific folder

I am trying to retrieve an email attachment and save it to specific directory in filesystem using the code below.
Dim objMail As Message = New Message(Encoding.ASCII.GetBytes(strMessage))
For Each att In objMail.FindAllAttachments()
Dim Stream as FileStream = New FileStream("D:\XX\" & att.FileName.ToString(),
FileMode.Create)
Dim BinaryStream As New BinaryWriter(Stream)
BinaryStream.Write(BitConverter.ToString(att.Body))
BinaryStream.Close()
Next
I have also tried att.GetBodyasText()
Using this code I am able to save attachment file in desired folder. But while opening a file, I am getting errorL
File is not in proper Format or not Decoded Properly.
I am new to MIME encoding/decoding.
I am a developer of OpenPop.NET.
There are multiple issues with the code you are using to instantiate the Message class:
Where is the strMessage contents comming from?
How do you know it is encoded in only ASCII?
These are two major issues that will likely make a big difference.
You should NOT be using a string to contain the message, instead you should be using raw bytes!
For example (in C#):
byte[] byteMessage = someFileStream.ReadToEnd();
Message message = new Message(byteMessage);
In this way, you will not destroy the message by using a wrong encoding on the bytes. Typically the email will include a header which tells how to decode to bytes to a string, which is what the OpenPop Message class will do for you.
Now let me explain attachments. Attachments are typically raw bytes, for example a PNG picture is some bytes that a PNG picture reader will understand. For the PNG picture reader to understand the picture, the attachments raw bytes must be saved to a file. You can get the raw bytes using att.Body.
There are also attachments where the raw bytes does not make sense - for example a text attachment encoded in BASE64 is not very useful for a text reader program, and such an attachment must be converted to text before saved. You can get the text using att.GetBodyAsText().
What you are doing is taking the raw bytes for the attachment and then using a BitConverter to convert it into hexadecimal numbers - which I cannot make any meaning of. Instead, you should change your:
BinaryStream.Write(BitConverter.ToString(att.Body))
to
BinaryStream.Write(att.Body)
in case your attachment is a picture or some more complex file.
I hope this can help with your problem.

copy image to clipboard and let it be pasted as file (vb.net)

I have a picture box, and if I use snipet below:
Clipboard.SetImage(PictureBox.image)
Then I can only paste the image into things like Paint and MS word. I can't paste it as a file into a folder/desktop.
So how can I copy the image to the clipboard and if gets pasted to a folder then it becomes a file?
If you're using .net and your ultimate goal is to save the file, there's a LOT easier way,
Here the code in C#, porting it into VB.net won't be hard, I'm just too lazy to do that :)
Anyway, you do have to save it somewhere before you can paste it so...
It loads the file to the Picture box and again saves it to a file, (lame, I know)
and set the clipboard data as a copy operation
then when you paste (Ctrl+V) it, it gets pasted.
C#
__
Bitmap bmp;
string fileName=#"C:\image.bmp";
//here I assume you load it from a file, you might get the image from somewhere else, your code may differ
pictureBox1.Image=(Image) Bitmap.FromFile(fileName);
bmp=(Bitmap)pictureBox1.Image;
bmp.Save(#"c:\image2.bmp");
System.Collections.Specialized.StringCollection st = new
System.Collections.Specialized.StringCollection();
st.Add(#"c:\image2.bmp");
System.Windows.Forms.Clipboard.SetFileDropList(st);
</pre>
and viola tries pasting in a folder the file image2.bmp will be pasted.
Here's basically what #Vivek posted but ported to VB. Up-vote his if this works for you. What you have to understand is that explorer will only allow you to paste files, not objects (AFAIK anyway). The reason is because if you copy image data to the clipboard, what format should it paste in? PNG, BMP, JPG? What compression settings? So like #Vivek said, you need to think those over, create a file on your own somewhere on the system and use SetFileDropList which will add the temp file to the clipboard.
' Add it as an image
Clipboard.SetImage(PictureBox1.Image)
'Create a JPG on disk and add the location to the clipboard
Dim TempName As String = "TempName.jpg"
Dim TempPath As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, TempName)
Using FS As New System.IO.FileStream(TempPath, IO.FileMode.Create, IO.FileAccess.Write, IO.FileShare.Read)
PictureBox1.Image.Save(FS, System.Drawing.Imaging.ImageFormat.Jpeg)
End Using
Dim Paths As New System.Collections.Specialized.StringCollection()
Paths.Add(TempPath)
Clipboard.SetFileDropList(Paths)