How to write string data to tiff file in vb.net - 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.

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 do I make an image not selectable

I have added an image to my iTextSharp PDF document like this:
Public Sub CreatePDFFromBitmap(ByVal uPath As String, ByVal uBitmap As Bitmap)
Dim nFs As System.IO.FileStream = New FileStream(uPath, FileMode.Create)
Dim nDocument As iTextSharp.text.Document
Dim nWriter As iTextSharp.text.pdf.PdfWriter
Dim nCb As iTextSharp.text.pdf.PdfContentByte
Dim nImgFromBitmap As System.Drawing.Image = DirectCast(uBitmap, System.Drawing.Image)
Dim nImg As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(nImgFromBitmap, Imaging.ImageFormat.Png)
Dim bLandscape As Boolean = (nImg.Width > nImg.Height)
'rotation needs to be set before document is being opened
If bLandscape Then
nDocument = New iTextSharp.text.Document(PageSize.A4.Rotate, 0, 0, 0, 0)
Else
nDocument = New iTextSharp.text.Document(PageSize.A4, 0, 0, 0, 0)
End If
'if an exception is raised here, the following will help: https://stackoverflow.com/questions/15833285/pdfwriter-getinstance-throws-system-nullreferenceexception
nWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(nDocument, nFs)
nDocument.Open()
nCb = nWriter.DirectContent
nImg.ScaleToFit(nDocument.PageSize.Width, nDocument.PageSize.Height) 'raises dpi size :-)))
'X-Y-Koordinatensystem 0,0 startet also unten links, nicht oben-links
nImg.SetAbsolutePosition(0, nDocument.PageSize.Height - nImg.ScaledHeight)
nCb.AddImage(nImg)
nDocument.Close()
nWriter.Close()
nFs.Close()
End Sub
It works fine.
However, when I click the image in the PDF, it gets selected.
This is not what I want.
If I click the image in the PDF, it should not be selected.
This is what it looks like: The image becomes blue:
I want to add editable fields to the PDF, so I need to make the image not selectable, else it would confuse the user.
As Abdel-Rahman Al-Qawasmi mentions in his answer, it is completely up to the PDF viewer which entities it makes selectable and which not. Thus, there is no guaranteed way to get what you want.
Nonetheless, there are ways to put an image into a PDF which dissuade current versions of most PDF viewers from making it selectable. These ways either transform the bitmap image into a non-bitmap entity (e.g. by iterating over the pixels of the bitmap and drawing a little rectangle per pixel using vector graphics) or wrap the bitmap image into something that usually is not selectable.
Let's take the latter approach and wrap the image into a page-size PDF pattern with which we then fill the actual page. You can do that by replacing your
nCb.AddImage(nImg)
by
Dim painter As iTextSharp.text.pdf.PdfPatternPainter = nCb.CreatePattern(nDocument.PageSize.Width, nDocument.PageSize.Height)
painter.AddImage(nImg)
nCb.SetColorFill(New iTextSharp.text.pdf.PatternColor(painter))
nCb.Rectangle(0, 0, nDocument.PageSize.Width, nDocument.PageSize.Height)
nCb.Fill()
(This essentially is the VB/iTextSharp pendant of the Java/iText code from this answer.)
This is a pdf program specifications and not related to asp.net or vb.net programming. you need to have control of the pdf reader settings. Or try to use another format.

Resizing multiple images and saving them to a separate folder

I need to resize and compress 200 images that I have stored in a folder.
I am getting these images in a list using this code that I got from another question:
Dim dir = New IO.DirectoryInfo("C:\\Users\\Charbel\\Desktop\\Images")
Dim images = dir.GetFiles("*.jpg", IO.SearchOption.AllDirectories).ToList
Dim pictures As New List(Of PictureBox)
For Each img In images
Dim picture As New PictureBox
picture.Image = Image.FromFile(img.FullName)
pictures.Add(picture)
Next
Now, I need to compress and reduce each image to (500x374) and then save them in another folder on my PC.
Well, let me first point out a couple of points about your code:
PictureBox doesn't serve any purpose here. You shouldn't create a PictureBox to use the Image.
Always remember to dispose the Image object (e.g., by wrapping it in a Using block) so you don't run into memory issues.
Unlike C#, VB.NET doesn't require escaping the \ character, therefore, you can write your path like this "C:\Users...".
Now, for resizing the image, you can simply create an instance of the Bitmap class with the constructor that takes an image and a size argument: Bitmap(Image, Size) or Bitmap(Image, Int32, Int32).
Here:
Dim sourcePath As String = "C:\Users\Charbel\Desktop\Images"
Dim outputPath As String = "C:\Users\Charbel\Desktop\Images\Resized"
IO.Directory.CreateDirectory(outputPath)
Dim dir = New IO.DirectoryInfo(sourcePath)
Dim files As IO.FileInfo() = dir.GetFiles("*.jpg", IO.SearchOption.AllDirectories)
For Each fInfo In files
Using img As Bitmap = Image.FromFile(fInfo.FullName)
Using resizedImg As New Bitmap(img, 500, 374)
resizedImg.Save(IO.Path.Combine(outputPath, fInfo.Name),
Imaging.ImageFormat.Jpeg)
End Using
End Using
Next

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

How can i crop multiples image with source directory and store it in same directory after cropping with same name?

I have try this code where alstFileName stores the names of files store in source directory:
For Each _file As String In alstFileName
Dim image As Bitmap = New Bitmap(_file)
Dim rect As New Rectangle(539, 345, 168, 124)
Dim cropped As Bitmap = image.Clone(rect, image.PixelFormat)
cropped.Save(_file, System.Drawing.Imaging.ImageFormat.Jpeg)
Next
It's give me error at line
cropped.Save(_file, System.Drawing.Imaging.ImageFormat.Jpeg)
tell me that A generic error occurred in GDI+.
What i should do?
Thanks in advance.