Using richtextbox instead of File.WriteBytes - vb.net

I am using this code now:
Dim data() As Byte = File.ReadAllBytes("D:\desktop\image.png")
File.WriteAllBytes("D:\desktop\file.txt", data)
Instead of File.WriteAllBytes (writing to a file) I want to write those bytes to a richtextbox, is this possible?
Thanks!

you can use like the following
Dim data() As Byte = File.ReadAllBytes("K:\sample.txt")
ritchText.Text = System.Text.Encoding.UTF8.GetString(data)
If you give the input as image file then you will get only Unicode characters. if you give textfile like the above then will give the exact content of the input file.

Related

How can i show the image name when i taking a picture?

Im trying to show the name of the image i take with my Camera but i dont know how.
Do i need a another function to show the image name?
My Code for taking picture:
Public Function takePicture() As String
Dim url = THETA_URL & "commands/execute"
Dim payload = New Dictionary(Of Object, Object) From {
{"name", "camera.takePicture"}
}
Dim request As Net.WebRequest = Net.WebRequest.Create(url)
request.Credentials = New Net.NetworkCredential(THETA_ID, THETA_PASSWORD)
Dim resp As Net.WebResponse = request.GetResponse()
End Function
See the line where is says:
End Function
Look in the line numbers margin; next to it there is a blank bar (mine is dark gray in my theme) - click in it to put a red dot:
End Function will go red too..
Then run your code and retrieve your image. The code will stop with a yellow bar pointing to End Function
Take a look at the bottom of VS - you'll either have an Autos window or a Locals window - either one will do. It will show the response object and you can drill into it like a tree, open the headers collection, have a look if anything in it contains the data you want.. It also thus tells you how to get the data you want out of it..
e.g. if I wanted the "Content-Disposition" value I could say resp.Headers("Content-Disposition") - AllKeys is showing me what available strings I can use to index the headers collection
Content-Disposition probably won't list a filename on its own - it'll be something more involved like "attachment; filename=someimage.jpg" so you'll need to pull the data you want out of it. Don't get your hopes up if this is a basic cam; it's unlikely to have any meaningful sort of filename. It might be IMG_0001 etc, if it's there at all - I think you should instead make your own name up, as you'll be able to put more info into it, it will be more meaningful than what you get from the cam (and if the cam doesn't send a filename you'll have to do it anyway)

apply text formatting from string to text for FlowDocument in richtextbox

I know that when adding text/content/DataContext in XAML you refer to resource dictionary or inline mark up for styling around text or in template.
Q:
However I'm having trouble trying to find a way to do the following:
Data is coming from a View Model/Model that is pulled from a database.
(string value)
I am a <Bold>smart</Bold> man.
to show in a flow document like this:
I am a smart man.
Q end
Either by binding to a converter, behavior, or would saving the paragraph/document that I put in the flow document to a .rtf file in memory stream be a better option?
I've tried to utilize the option for behavior listed > here < but that is for text block and unable to redirect for type text instead of text block.
Trying to make it streamlined.
Tried to use data binding and apply the converter but even though I have the resource for the behavior / converter, it work due to the type conversion.
One clever solution is presented by Rockford Lhotka in post Set rich text into RichTextBlock control. His idea is to create a custom control which then creates the RichTextBlock using XamlReader.Load.
This allows you to use code like the following:
<local:RichTextDisplay Xaml="{Binding Hello}" HorizontalAlignment="Center"
VerticalAlignment="Center"/>
Where Hello is:
public string Hello { get; set; } = "I am a <Bold>smart</Bold> man.";
With a result:
If you use UWP/Win 8.1 XAML, you can use the original code from the blog post with the following small change (Paragraphs added):
<UserControl
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:mc=""http://schemas.openxmlformats.org/markup-compatibility/2006"">
<Grid>
<RichTextBlock><Paragraph>");
xaml.Append(ctl.Xaml);
xaml.Append(#"
</Paragraph></RichTextBlock>
</Grid>
</UserControl>
");
To answer my own question:
My case was creating a Document style display for user to update and save as a PDF, but I didn't want to rely on Office being on our application Server.
So I resolved this in my case by using a full "doc.RTF" document and importing that as a memory stream/string and apply my needed updates for values to that.
i.e. VB.net snippet example
Using uStream = Assembly.GetExecutingAssembly.GetManifestResourceStream("Resourcefilepath.rtf")
Using mStream As system.IO.MemoeryStream = New MemoryStream()
uStream.CopyTo(mStream)
rtfstring = Encoding.UTF8.GetSTring(mStream.toArray())
'--Do the updates to the needed string as needed:
rtfstring.Replace("Value","UpdatedValue")
'--Load Property Memory String this method is returnind
RTFDataProperty = New MemoryStream(Encoding.UTF8.GetBytes(rtfstring))
End Using
End Using
Then I loaded my XAML Rich Text Box with that memory stream as DataFormats.Rtf.
RichTextBox1.SelectAll()
RichTextBox1.Selection.Load(ClassName.RTFDataProperty, DataFormats.Rtf)
This gave me a template for formatting and layout of that document. (More of a case scenario and not a normal practice)
I also wanted to apply a starting selection so here is what I did there:
'--Get my RichTextBox Text
rtbtext As String = New TextRange(RichTextBox1.Document.contentStart, RichTextbox1.Document.ContentEnd).Text
Dim strStartSelection As String = "Comments..."
Dim startTP As TextPointer
Dim endTP As TextPointer
'--Loop through the paragraphs of the richtextbox for my needed selection starting point:
For Each para As Paragraph In RichTextBox1.Document.Blocks
Dim paraText As String = New TextRange(para.ContentStart, para.ContentEnd).Text
If paraText = "" Then
Dim pos As TextPointer = para.ContentStart
startTP = pos
endTP = startTP.GetPositionAtOffset("".Length + 3) '--my string had ... on the end so had to add for avoiding the escape of that on length
RichTextBox1.Selection.Select(startTP, endTP)
RichTextBox1.Focus()
Exit For
End If
Next
This is the simple VB.net code layout, but you can simplify and adjust from there if you find it useful.
Thanks

VB.NET - How to correctly load a resource icon with API

I need to extract an icon resource from a file. I have a structure (Resource) that contains the byte array, size, find handle, load handle, lock handle (from FindResource, LoadResource and LockResource, respectively).
I know that I need to obtain the Icon handle of my icon. Then I need to use GetIconInfo to retrieve the bit-mask. Then I use Image.FromHbitmap(h) to get an image. I then use the dimensions of the image as parameters to CreateIconFromResourceEx to retrieve a properly sized Icon.
Does anyone have some code to do this? I can declare all P/Invoke myself. Thanks!
My original code is returning a 32x32 every time.
Dim out As ICONINFO
Dim h As IntPtr = res.hLock
GetIconInfo(h, out)
Dim s As Image = Image.FromHbitmap(out.hbmMask)
h = ResourceExplorer.CreateIconFromResourceEx(res.bArray, res.hSize, True, &H30000, s.Width, s.Height, 0)
PictureBox1.Image = Icon.FromHandle(h).ToBitmap

using DataURL in VB.NET

happy new year!
I just want to generate DataURL type data in VB.Net and also want to load string type DataURL to an image.
In other words, I want to convert an image in PictureBox(or Image) into string type DataURL vice versa.
The purpose of this kind of doubtful procedure is to save string data and image data into one single file.
Thank you!
Something like this:
Use this function to get a Base64 string representation of the Image
Public Function ToBase64String(ByVal aImage As Image) As String
Using stream = New System.IO.MemoryStream
aImage.Save(stream, System.Drawing.Imaging.ImageFormat.Png)
Return Convert.ToBase64String(stream.ToArray)
End Using
End Function
Then use this to display it:
<img src="data:image/png;base64, Base64StringHere" />

Loading byte data to a picture box

I have saved an image in database using following code.
ofd.ShowDialog()
vrPicHolder = IO.File.ReadAllBytes(ofd.FileName)
Dim drPic As DataRow
drPic = DsPic.tblPicTest.NewRow
drPic.Item("Picture") = vrPicHolder
DsPic.tblPicTest.Rows.Add(drPic)
taPic.Update(DsPic.tblPicTest)
Now I want to display this image in a picture box. I tried
PictureBox1.Image = Image.FromFile(vrPicHolder)
But it says can not convert Byte() to string. Please advise how to load this picture.
Thanks
Furqan
The method you're calling expects a string that's the filename of an image. You need to pass it the filename of an image that is a BMP, GIF, JPEG, PNG or TIFF format.
Image.FromFile Method (String)
What you should be doing is this:
Dim pictureBytes as New MemoryStream(vrPicHolder)
PicutureBox1.Image = Image.FromStream(pictureBytes)
Image.FromStream Method (Stream)
This is because Image.FromFile Method expects FilePath as String. Its like importing a file from some given path.See Image.FromFile Method (String)
Image.FromStream Method (Stream) is the remedy for this.
One more thing ,you should not set the complete Image to the database . Instead save the image into some physical path and refer this path from the database.
Check this out:
http://windevblog.blogspot.com/2008/08/convert-image-to-byte-array-and-vice.html