I asked earlier about a way to add a watermark to a picture using visual basic . and thanks to everybody i managed to find a way , but now it seems like I need a code for an option to make the user select the coordinates of the new watermark position on the picture.
Here is my code
Dim Image1 As New Bitmap(OpenFileDialog1.FileName)
Dim Image2 As New Bitmap(PictureBox1.Image)
Dim Result As New Bitmap(Math.Max(Image1.Width, Image2.Width), _
Image1.Height + Image2.Height)
Dim gResult As Graphics = Graphics.FromImage(Result)
gResult.DrawImage(Image1, New Point(0, 0))
gResult.DrawImage(Image2, New Point(0, Image1.Height))
gResult.Dispose()
gResult = Nothing
PictureBox5.Image = Result
i found a way to do it . you can use 2 pictureboxes and add a code to move the watermark picturebox . you can save everything at the end using a screenshot method .
Related
I'm trying to convert some itextsharp code to use itext7 which stamps text on each page of a pdf at rotate 90 degrees. Unfortunately all the examples I can find are in c# and while I can use an online translator I'm having difficulties with this one.
The below code stamps my text on at the specified coords on each page of a given pdf:
Shared Sub itext7_stamp_text_on_pdf(mypdfname As String, myfoldername As String)
Dim src As String = myfoldername & "\" & mypdfname
Dim dest As String = myfoldername & "\Stamped " & mypdfname
Dim pdfDoc As PdfDocument = New PdfDocument(New PdfReader(src), New PdfWriter(dest))
Dim document As Document = New Document(pdfDoc)
Dim canvas As PdfCanvas
Dim n As Integer = pdfDoc.GetNumberOfPages()
For i = 1 To n
Dim page As PdfPage = pdfDoc.GetPage(i)
canvas = New PdfCanvas(page)
With canvas
.SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.HELVETICA), 12)
.BeginText()
.MoveText(100, 100)
.ShowText("SAMPLE TEXT 100,100")
.EndText()
End With
Next
pdfDoc.Close()
End Sub
... but I can't see a way of rotating it to 90 degrees.
There's an example here if you use a paragraph:
https://kb.itextpdf.com/home/it7kb/examples/itext-7-building-blocks-chapter-2-rootelement-examples#iText7BuildingBlocksChapter2:RootElementexamples-c02e14_showtextaligned
... but I can't seem to translate this to vb.net. I can specify where the errors I get are, but I thought I'd be better asking this general question first in case there's a way to do this without using a paragraph.
Can anyone help please?
Thanks!
Well, after some more digging this code seems to work OK on the rotation part:
Dim pdf As New PdfDocument(New PdfReader(inpdf), New PdfWriter(outpdf))
Dim document As New Document(pdf)
document.ShowTextAligned("This is some test text", 400, 750, TextAlignment.CENTER, VerticalAlignment.MIDDLE, 0.5F * CSng(Math.PI))
document.Close()
End Sub
.... but it gets hidden behind existing content, so I need a way to make sure it's set to over content.
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.
A part of this question concerns doing the reverse of the following one:
VB.net get location of userControl in another container
No matter what I do, I cannot seem to pinpoint the location of my chart inside a userform (D inside A), therefore what I save is something very different than the chart alone.
So, provided that my userform is too like this:
I need to save a screenshot of D, being D a polar chart with a title and a legend.
My code is:
Dim PicFile As String = FilesFolder & "Pic.png"
Dim myBounds As Rectangle = A.D.Bounds
Dim BitMap As New Bitmap(myBounds.Width, myBounds.Height)
Dim PtChart As Point
PtChart = A.PointToScreen(New Point(0, 0))
PtChart = A.D.PointToClient(PtChart)
Using g As Graphics = Graphics.FromImage(BitMap)
g.CopyFromScreen(PtChart, Point.Empty, myBounds.Size)
End Using
BitMap.Save(PicFile, System.Drawing.Imaging.ImageFormat.Png)
Why does this not save the chart (D) correctly?
BONUS: how can I make it work even when I have another application open (say, internet browser) on top of the userform?
I have added a datagridview on a windows form with the name DataGridView1. The following code adds a row with 2 columns. I want to show an image in the 2nd column.
Dim dt As New DataTable
dt.Columns.Add("TESTROW")
dt.Rows.Add("TESTCONTENT")
DataGridView1.DataSource = dt
Dim colImage As New DataGridViewImageColumn
DataGridView1.Columns.Add(colImage)
For intI As Integer = 0 To dt.Rows.Count
Dim cellImage As New DataGridViewImageCell
' THE FOLLOWING LINE WORKS FINE!!!!
cellImage.Value = Drawing.Image.FromFile("c:\foo\bar.gif")
' BUT WHY NOT THIS?
' cellImage.Value = Properties.Resources.ResourceManager.GetObject("ExistingRessource")
' OR THIS?
' cellImage.Value = CType(Properties.Resources.ResourceManager.GetObject("ExistingRessource"), Image)
cellImage.ImageLayout = DataGridViewImageCellLayout.Zoom
DataGridView1.Rows(intI).Cells(1) = cellImage
Next
It's working fine if I use "fromFile" with the path to the image and the 2nd column shows the gif picture inside the cell.
Unfortunately, my attempt to load an image from the ressource ("GetObject") fails, and the cell shows a page-symbol with a red cross on it.
I got all the images I need inside the ressource.
How can I achieve this?
Thanks in advance.
If you've added an image to the resources, then you can access it with my.resources.ResourceName where ResourceName seems to be "ExistingRessource" in your case
It's possible that your current attempts are failing because the resource isn't actually added properly, or you have got the spelling wrong on the name? Either way if you use my.resources you can see for certain that the resource is added properly.
cellImage.Value = My.Resources.ExistingRessource
I am creating questionnaires using VB.NET and SQL and my problem now is how can I show an image already present in the database onto a form? Please note that as the form is a questionnaire, it is navigable by the means of going up or down rows in a dataset. For example when the NEXT button is clicked, the row in the dataset goes +1. So how should I go about coding it in order to display an image onto the form?
this is the code how i saved the image
Dim ms As New MemoryStream()
PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
Dim data As Byte() = ms.GetBuffer()
Dim x As New SqlParameter("#image", SqlDbType.Image)
x.Value = data
cmd.Parameters.Add(x)
And the code for navigating between rows of a dataset in a single form:
RichTextBox1.Text = dsquestionnaire.Tables(0).Rows(qsno).Item("Question") .....
qsno + 1 (As a part of the NEXT button click event)
Thanks in advance..
You can load the image from the DB into your picture box as follows:
Using ms As New IO.MemoryStream(CType(row("image", Byte()))
Dim img As Image = Image.FromStream(ms)
Image1.Image = img
For the click event you can place this inside a method and just call it to load the image into the Picture box when the next button is clicked