I created a form with a PictureBox. I created a drawing in the PictureBox. But I can't save the file to a specified folder without dialogue controls. Only through code. Please help me if any one knows a solution for this problem. Thank you....
if you drawing in picture box you must convert drawing in bmp format with this code:
'Save as BMP-file
Dim bmp As New Bitmap(picturebox1.Width, picturebox1.Height)
draw.DrawToBitmap(bmp, New Rectangle(0, 0, picturebox1.Width, picturebox1.Height))
bmp.Save("D:\output.png", Imaging.ImageFormat.Png)
bmp.Dispose()
with this code you can print & save drawing on picturebox object
PictureBox has an Image property of type System.Drawing.Image which you use to get or set the image that the PictureBox represents. System.Drawing.Image has a Save method which will write the image to the specified filename or stream. Is this information OK or do you need more; your question was a little unclear.
Related
This question already has answers here:
How can I save a Panel in my form as a picture?
(2 answers)
How to print hidden and visible content of a Container with ScrollBars
(1 answer)
Closed last year.
I'm writing a simple utility to help teachers create sentence diagrams, which are contained in a standard Panel control. To save the diagram I am doing this:
Private Sub cmdSave_Click(sender As Object, e As EventArgs) Handles cmdSave.Click
dlgSave.DefaultExt = "png"
dlgSave.Filter = "PNG files (*.png)|*.png|All files (*.*)|*.*"
Dim result As DialogResult = dlgSave.ShowDialog()
If result <> DialogResult.Cancel Then
Dim bounds As Rectangle = DisplayPanel.Bounds
Dim pt As Point = DisplayPanel.PointToScreen(bounds.Location)
Dim bitmap As New Bitmap(bounds.Width, bounds.Height, Imaging.PixelFormat.Format32bppArgb)
Using g As Graphics = Graphics.FromImage(bitmap)
g.CopyFromScreen(New Point(pt.X, pt.Y), Point.Empty, bounds.Size, CopyPixelOperation.SourceCopy)
End Using
bitmap.Save(dlgSave.FileName, Imaging.ImageFormat.Png)
End If
End Sub
The result is an incomplete image. Here's what the form with the panel looks like and what the saved image looks like:
Complete form
Incomplete copy of panel
I should mention that the text in the panel is actually contained in label controls, so it wouldn't surprise me if that wasn't included and I would need to find a fix. But I'm puzzled as to why the entire panel client area (at least) isn't getting into the saved image. Any help would be greatly appreciated.
Edit: fixed thanks to a kind user who pointed me to a similar post. Turns out I should have been using DrawToBitmap and resetting the bounds to 0, like so
Dim bounds As Rectangle = DisplayPanel.Bounds
Dim bmp As Bitmap = New Bitmap(DisplayPanel.Width, DisplayPanel.Height)
bounds.X = 0
bounds.Y = 0
DisplayPanel.DrawToBitmap(bmp, bounds)
bmp.Save(dlgSave.FileName, Imaging.ImageFormat.Png)
New edit: the above works for the visible area, but not if content extends beyond it. Jimi's solution (see comments, and many thanks) covers the entire scrollable area but does not include lines drawn. If I can fix this I'll post result in case anyone finds it useful.
New Edit: solution posted in comments.
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.
I have a panel which draws a diagram based on user input. Unfortunately the diagrams can get really really big, and when I print them it doesn't entirely fit on a single page without losing readability.
I'm using PDFsharp within VB.net to create the PDF. The current method is the turn the panel in which the diagram is drawn on to a bitmap image, and then place the image in the pdf document.
1 diagram can easily make the panel 1500,3000 in size.
Example of Diagram
Example of Diagram in PDF
Any help is greatly appreciated
Dim pdfdoc As New PdfDocument
Dim page As PdfPage = pdfdoc.AddPage
Dim Bitmap As Bitmap = New Bitmap(pnl_Draw.Width, pnl_Draw.Height)
Dim BXImage As XImage
Dim GFX As XGraphics
Me.pnl_Draw.DrawToBitmap(Bitmap, New Rectangle(0, 0, Bitmap.Width, Height))
Dim pbx As New PictureBox
pbx.Image = Bitmap
BXImage = XImage.FromGdiPlusImage(pbx.Image)
GFX = XGraphics.FromPdfPage(page)
GFX.ScaleTransform(0.82)
GFX.DrawImage(BXImage, 0, 0)
GFX.Dispose()
pdfdoc.Save("G:\test.pdf")
pdfdoc.Close()
Try
Process.Start("G:\test.pdf")
Catch ex As Exception
End Try
Simple solution: in the Adobe Reader Print dialog click on "Poster" to print the image on more than 1 page. Increase the Zoom factor to distribute the poster on as many pages as you need.
You can also increase the page size proportionally to the bitmap size.
BTW: You don't need GFX.ScaleTransform(0.82) if you specify the destination size in the call to GFX.DrawImage.
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 change the background color of a picture saving from a picturebox in vb.net.In my form there is a drawing section.after drawing i am saving the picture as jpeg.but the image's background color is black.so i can't see anything that i have drawn.the drawing pen color is also black.if anyone knows please help me.thank you.
I would say that the easiest way is to paint the background when the image object is created (sample initializing the background to being white):
Dim theImage As Image = New Bitmap(someWidth, someHeight)
Using g As Graphics = Graphics.FromImage(theImage)
g.Clear(Color.White)
End Using
To use this the following code:
OpenFileDialog1.Filter = "Bmp Files(*.bmp)|*.bmp|Gif Files(*.gif)|*.gif|Jpg Files(*.jpg)|*.jpg"
OpenFileDialog1.ShowDialog()
Textbox1.Text = OpenFileDialog1.FileName
PicText.Image = Image.FromFile(OpenFileDialog1.FileName)