When inside the tool I want to text included in the image between the place where I want to speak, as the following picture:
enter image description here
I tried the following code but the photo does not show the speech, but end of the sentence appears:
Dim para As New Paragraph()
Dim bitmap As New BitmapImage(New Uri("D:\Happy.png"))
Dim image As New Image()
image.Source = bitmap
image.Width = 20
para.Inlines.Add(image)
RTB.Document.Blocks.Add(para)
See this link for examples Inline Images or Other Elements.
'A RichTextBox with an image.
Private Sub ImageRTB()
'Create a new RichTextBox.
Dim MyRTB As New RichTextBox()
' Create a Run of plain text and image.
Dim myRun As New Run()
myRun.Text = "Displaying text with inline image"
Dim MyImage As New Image()
MyImage.Source = New BitmapImage(New Uri("flower.jpg", UriKind.RelativeOrAbsolute))
MyImage.Height = 50
MyImage.Width = 50
Dim MyUI As New InlineUIContainer()
MyUI.Child = MyImage
' Create a paragraph and add the paragraph to the RichTextBox.
Dim myParagraph As New Paragraph()
MyRTB.Blocks.Add(myParagraph)
' Add the Run and image to it.
myParagraph.Inlines.Add(myRun)
myParagraph.Inlines.Add(MyUI)
'Add the RichTextBox to the StackPanel.
MySP.Children.Add(MyRTB)
End Sub
The solution found and this is the code:
Dim tp As TextPointer = rtb.CaretPosition.GetInsertionPosition(LogicalDirection.Forward)
Dim bm As New BitmapImage()
bm.BeginInit()
bm.UriSource = New Uri("Happy.png", UriKind.Relative)
bm.CacheOption = BitmapCacheOption.OnLoad
bm.EndInit()
Dim img As New Image()
img.Source = bm
img.Width = 20
img.Height = 20
img.Stretch = Stretch.Fill
Dim container As New InlineUIContainer(img, tp)
thank you :)
Related
I inserted an image into a .mdb database with this code from my WPF app :
Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & GetCurrentDirectory() & "\Data\rctts.mdb;Jet OLEDB:Database Password=arn33423342;")
con.Open()
Dim cmd As New OleDbCommand("Insert into recents(Uname,Pic,Email)values(#uname,#pic,#email)", con)
image1.Source = New BitmapImage(New Uri("D:\logo.png"))
Dim buffer As Byte()
Dim bitmap = TryCast(image1.Source, BitmapSource)
Dim encoder = New PngBitmapEncoder()
encoder.Frames.Add(BitmapFrame.Create(bitmap))
Using stream = New MemoryStream()
encoder.Save(stream)
buffer = stream.ToArray()
End Using
cmd.Parameters.AddWithValue("#pic", buffer)
The Pic column or cell's data type is OLE Object...Anyway,after inserting the data,i opened my database,i saw that a new record was added but the value of the Pic column was Long binary data.Anyway,then i went on retrieving the image in my wpf app.I used this code ?
Dim cmd As New OleDbCommand("Select * from recents", con)
Dim table As New DataTable
Dim adap As New OleDbDataAdapter(cmd)
adap.Fill(table)
If table.Rows.Count <= 0 Then
Else
For Each row In table.Rows
recentbtn.Image.ImageSource = BytesToImage(CType(row(1), Byte()))
recentbtn.Names.Text = table.Rows(0)(0).ToString
AddHandler recentbtn.MouseDown, AddressOf recentbtn_mousedow
RecentsList.stp.Children.Add(recentbtn)
Next
End If
loadingrecents.Visibility = Visibility.Hidden
End Sub
Private Shared Function BytesToImage(ByVal bytes As Byte()) As BitmapImage
Dim bm = New BitmapImage()
Using stream As MemoryStream = New MemoryStream(bytes)
stream.Position = 0
stream.Seek(0, SeekOrigin.Begin)
bm.BeginInit()
bm.StreamSource = stream
bm.CreateOptions = BitmapCreateOptions.PreservePixelFormat
bm.CacheOption = BitmapCacheOption.OnLoad
bm.EndInit()
End Using
Return bm
End Function
But it's returning an error : No imaging component suitable to complete the operation was found
Fixed it...For anyone who faces this error in future :
Make sure you're inserting the data in the proper way(sometimes corrupted data in the db causes such errors)
2 . You don't need to do some heavy coding to convert the image to byte!
Finally,let's code :
Public Sub read()
con.Open()
Dim cmd As New OleDbCommand("Select * from recents", con)
Dim _dr As OleDbDataReader
_dr = cmd.ExecuteReader
Dim _photo As Byte()
While _dr.Read()
Try
_photo = CType(_dr(1), Byte())
Dim strm As MemoryStream = New MemoryStream(_photo)
Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(strm)
Dim bi As BitmapImage = New BitmapImage()
bi.BeginInit()
Dim ms As MemoryStream = New MemoryStream()
img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
ms.Seek(0, SeekOrigin.Begin)
bi.StreamSource = ms
bi.EndInit()
image1.Source = bi
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End While
I have some images that I'am trying to print out. Those images can come in varying format, from different DPI's to different formats (JPEG, PNG, etc.)
Now what I've done for now, is to load the image into my application and try and
convert the dpi to say 96. However in this process i get an OutOfMemoryException, and I'm not sure how to continue.
Private Sub PrintImage(Optional providedPrintDialog As PrintDialog = Nothing)
Dim objPrintDialog As PrintDialog
If providedPrintDialog IsNot Nothing Then
objPrintDialog = providedPrintDialog
Else
objPrintDialog = New PrintDialog()
End If
Dim myPanel As New StackPanel
myPanel.Margin = New Thickness(15)
Dim myImage As New Controls.Image
Dim tempBitmapImage = ConvertBitmapToXDPI(Me.SelectedFileViewModel.File.GetPath, 96)
Dim tempBitmapImageWidth As Integer = CInt(objPrintDialog.PrintableAreaWidth)
' A4 max width = 793
If tempBitmapImage.Width > tempBitmapImageWidth Then
myImage.Stretch = System.Windows.Media.Stretch.Uniform
Else
myImage.Stretch = System.Windows.Media.Stretch.None
End If
myImage.Source = tempBitmapImage
myPanel.Children.Add(myImage)
myPanel.Measure(New System.Windows.Size(objPrintDialog.PrintableAreaWidth, objPrintDialog.PrintableAreaHeight))
myPanel.Arrange(New Rect(New System.Windows.Point(0, 0), myPanel.DesiredSize))
objPrintDialog.PrintVisual(myPanel, "Billede") ' <- OutOfMemoryException thrown here
End Sub
Private Function ConvertBitmapToXDPI(path As String, newDpi As Integer) As BitmapSource
Using bitmap As Bitmap = DirectCast(System.Drawing.Image.FromFile(path), Bitmap)
Dim bitmapData = bitmap.LockBits(New System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.[ReadOnly], bitmap.PixelFormat)
Dim bmSource = BitmapSource.Create(
bitmapData.Width,
bitmapData.Height, 96, 96, PixelFormats.Bgr24, Nothing,
bitmapData.Scan0,
bitmapData.Stride * bitmapData.Height,
bitmapData.Stride)
bitmap.UnlockBits(bitmapData)
Return bmSource
End Using
End Function
There is no need to do any DPI conversion. Just create a DrawingVisual and draw a BitmapImage into it with an appropriate size:
Dim image As New BitmapImage()
image.BeginInit()
image.CacheOption = BitmapCacheOption.OnLoad
image.UriSource = New Uri(path)
image.EndInit()
image.Freeze()
Dim size As New Size()
If image.Width < printDialog.PrintableAreaWidth Then
size.Width = image.Width
size.Height = image.Height
Else
size.Width = printDialog.PrintableAreaWidth
size.Height = size.Width / image.Width * image.Height
End If
Dim visual As New DrawingVisual()
Using dc As DrawingContext = visual.RenderOpen()
dc.DrawImage(image, New Rect(size))
End Using
printDialog.PrintVisual(visual, "Billede")
I have a number of images in the resources directory and access them like this...
Public Function ReturnToolBarImage(ByVal ImageName As String) As Image
Dim UpdateImage As New Image
With UpdateImage
End With
Dim UpdateBitmap As New BitmapImage
With UpdateBitmap
.BeginInit()
.UriSource = New Uri("pack://application:,,,/HOA_Manager_Client_04;component/Resources/" & ImageName, UriKind.Absolute)
.EndInit()
.DecodePixelHeight = 32
End With
UpdateImage.Source = UpdateBitmap
Return UpdateImage
End Function
I have control that insists upon using an Image Path not an Image - I can access it like this
.ImageSource = "//application:,,,/HOA_Manager_Client_04;component/Resources/Customers.png"
But the image is, or course, far too large.. Soooooo I added a function to save the resized image to a Temp folder (that is working) and send the path back - but for some reason that just doesn't work (no errors, just no image). Any ideas?
Public Function ReturnToolBarImageAsString(ByVal ImageName As String) As String
Dim UpdateImage As New Image
With UpdateImage
End With
Dim UpdateBitmap As New BitmapImage
With UpdateBitmap
.BeginInit()
.UriSource = New Uri("pack://application:,,,/HOA_Manager_Client_04;component/Resources/" & ImageName, UriKind.Absolute)
.EndInit()
.DecodePixelHeight = 32
End With
Using FS As New IO.FileStream("../../Pages/Temp/" & ImageName, IO.FileMode.Create)
Dim vEncoder As New PngBitmapEncoder
vEncoder.Frames.Add(BitmapFrame.Create(UpdateBitmap))
vEncoder.Save(FS)
End Using
'Return "../Temp/" & ImageName
Return "//application:,,,/HOA_Manager_Client_04;component/Pages/Temp/" & ImageName
End Function
By creating a directory at run time in the Bin directory seems to have cured the issue
Public Function ReturnToolBarImageAsString(ByVal ImageName As String) As String
Dim UpdateImage As New Image
With UpdateImage
End With
Dim UpdateBitmap As New BitmapImage
With UpdateBitmap
.BeginInit()
.DecodePixelHeight = 32
.UriSource = New Uri("pack://application:,,,/HOA_Manager_Client_04;component/Resources/" & ImageName, UriKind.Absolute)
.EndInit()
End With
Dim vPath As String = My.Application.Info.DirectoryPath
If Not IO.Directory.Exists(vPath & "\Temp_Images") Then
IO.Directory.CreateDirectory(vPath & "\Temp_Images")
End If
Using FS As New IO.FileStream(vPath & "\Temp_Images\" & ImageName, IO.FileMode.Create)
Dim vEncoder As New PngBitmapEncoder
vEncoder.Frames.Add(BitmapFrame.Create(UpdateBitmap))
vEncoder.Save(FS)
End Using
Return vPath & "\Temp_Images\" & ImageName
End Function
This is my first post and I'm really frustrated using Silverlight just because I'm a newbie in this.
I have three Silverlight UI (StackPanels basically) "stkMain1", "stkMain2" and "stkMain3".
I have to convert these three stack panels to PDF. I'm using silverPDF (I guess it further uses iTextSharp and PDFSharp.)
I've written the following code :
Private Sub cmdImage_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles cmdImage.Click
Dim d As New SaveFileDialog()
d.Filter = "PDF file format|*.pdf"
' Save the document...
If d.ShowDialog() = True Then
stkMain.Children.Clear()
stkMain.Children.Add(stkMain1)
Dim document As New PdfDocument()
Dim page As PdfPage = document.AddPage
Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
Dim img As ImageTools.ExtendedImage = BillPage1.ToImage
Dim mstream As New MemoryStream()
Dim encoder As New JpegEncoder()
encoder.Encode(img, mstream)
mstream.Seek(0, SeekOrigin.Begin)
Dim pdfImg As XImage = XImage.FromStream(mstream)
gfx.DrawImage(pdfImg, 0, 0)
End If
End Sub
This does everything correct and gives one PDF file with one page.. Superb output and Thumbs up.
Now the real problem starts:
Private Sub cmdImage_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles cmdImage.Click
Dim d As New SaveFileDialog()
d.Filter = "PDF file format|*.pdf"
' Save the document...
If d.ShowDialog() = True Then
stkMain.Children.Clear()
stkMain.Children.Add(BillPage1)
Dim document As New PdfDocument()
Dim page As PdfPage = document.AddPage
Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
Dim img As ImageTools.ExtendedImage = BillPage1.ToImage
Dim mstream As New MemoryStream()
Dim encoder As New JpegEncoder()
encoder.Encode(img, mstream)
mstream.Seek(0, SeekOrigin.Begin)
Dim pdfImg As XImage = XImage.FromStream(mstream)
gfx.DrawImage(pdfImg, 0, 0)
document.Pages.Add()
Dim page1 As PdfPage = document.AddPage
Dim gfx1 As XGraphics = XGraphics.FromPdfPage(page1)
Dim img1 As ImageTools.ExtendedImage = BillPage2.ToImage
Dim mstream1 As New MemoryStream()
Dim encoder1 As New JpegEncoder()
encoder1.Encode(img1, mstream1)
mstream1.Seek(0, SeekOrigin.Begin)
Dim pdfImg1 As XImage = XImage.FromStream(mstream1)
gfx1.DrawImage(pdfImg1, 0, 0)
document.Save(d.OpenFile())
End If
End Sub
Now this creates Two pages, and I get a scrambled output, contents of the stackpanels overlapping each other.
How to solve this?? I NEED HELP DESPERATELY. THE PROJECT IS DUE AND I HAVE TO SUBMIT IT BY 26th March 2011 (MONDAY).
Thanks in advance
Ravi
Try this:
If d.ShowDialog() = True Then
Dim document As New PdfDocument()
Dim page As PdfPage = document.AddPage
Dim pdfImg As XImage = DrawUI(stkMain1)
gfx.DrawImage(pdfImg, 20, 20)
page = document.AddPage()
gfx = XGraphics.FromPdfPage(page)
pdfImg = DrawUI(stkMain2)
page = document.AddPage()
gfx = XGraphics.FromPdfPage(page)
gfx.DrawImage(pdfImg, 20, 20)
pdfImg = DrawUI(stkMain3)
gfx.DrawImage(pdfImg, 20, 20)
document.Save(d.OpenFile())
End If
Private Function DrawUI(ByVal oControl As Object) As XImage
Dim img As ImageTools.ExtendedImage = ImageExtensions.ToImage(oControl)
Dim mstream As New MemoryStream()
Dim encoder As New JpegEncoder()
encoder.Encode(img, mstream)
mstream.Seek(0, SeekOrigin.Begin)
Dim pdfImg As XImage = XImage.FromStream(mstream)
Return pdfImg
End Function
Can multiple xps documents be merged to one xps document in WPF and shown in DocumentViewer?
An application has 4 small xps documents each displayed seperately, but in one of the places all 4 documents should be shown as one document. How do I go about it?
Here, targetDocument is the target path of the new file and list is a list of all documents to be merged.
public void CreateXPSStreamPages(string targetDocument, List<string> list)
{
Package container = Package.Open(targetDocument, FileMode.Create);
XpsDocument xpsDoc = new XpsDocument(container);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
SerializerWriterCollator vxpsd = writer.CreateVisualsCollator();
vxpsd.BeginBatchWrite();
foreach (string sourceDocument in list)
{
AddXPSDocument(sourceDocument, vxpsd);
}
vxpsd.EndBatchWrite();
container.Close();
}
public void AddXPSDocument(string sourceDocument, SerializerWriterCollator vxpsd)
{
XpsDocument xpsOld = new XpsDocument(sourceDocument, FileAccess.Read);
FixedDocumentSequence seqOld = xpsOld.GetFixedDocumentSequence();
foreach (DocumentReference r in seqOld.References)
{
FixedDocument d = r.GetDocument(false);
foreach (PageContent pc in d.Pages)
{
FixedPage fixedPage = pc.GetPageRoot(false);
double width = fixedPage.Width;
double height = fixedPage.Height;
Size sz = new Size(width, height);
fixedPage.Width = width;
fixedPage.Height = height;
fixedPage.Measure(sz);
fixedPage.Arrange(new Rect(new Point(), sz));
//fixedPage.UpdateLayout();
ContainerVisual newPage = new ContainerVisual();
newPage.Children.Add(fixedPage);
//test: add Watermark from Feng Yuan sample
//newPage.Children.Add(CreateWatermark(width, height, "Watermark"));
vxpsd.Write(newPage);
}
}
xpsOld.Close();
}
In case someone is interested in the VB code:
Public Sub CreateXPSStream(targetDocument As String, ListToMerge As List(Of String))
If (File.Exists(targetDocument)) Then
File.Delete(targetDocument)
End If
Dim container As Package = Package.Open(targetDocument, FileMode.Create)
Dim xpsDoc = New System.Windows.Xps.Packaging.XpsDocument(container)
Dim seqNew As FixedDocumentSequence = New FixedDocumentSequence()
For Each sourceDocument As String In ListToMerge
AddXPSDocuments(sourceDocument, seqNew)
Next
Dim xpsWriter As XpsDocumentWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc)
xpsWriter.Write(seqNew)
xpsDoc.Close()
container.Close()
End Sub
Public Sub AddXPSDocuments(ByVal sourceDocument As String, ByRef seqNew As FixedDocumentSequence)
Try
Dim xpsOld As XpsDocument = New XpsDocument(sourceDocument, FileAccess.Read)
Dim seqOld As FixedDocumentSequence = xpsOld.GetFixedDocumentSequence()
For Each r As DocumentReference In seqOld.References
Dim newRef As DocumentReference = New DocumentReference()
CType(newRef, IUriContext).BaseUri = CType(r, IUriContext).BaseUri
newRef.Source = r.Source
seqNew.References.Add(newRef)
Next
Catch ex As Exception
myStatusAdd("Error with " & sourceDocument)
End Try
End Sub
thanks for the initial code.
This requires also a fair amount of references in your projects:
PresentationCore
PresentationFramework
ReachFramwork
System.Core
System.Windows.Presentation
System.Xaml
System.Printing
WindowsBase
I think that's all.