Okay, I've learned a bit more and have rephrased my question. I've got a need to flip or invert text 180 degrees (so it appears upside-down) on a RDLC report. I have some custom VB code that takes the text, converts it to a bitmap, then flips the rotates the canvas 180 degrees. The effect of this makes the text look a bit.. dithered... or fuzzy. It's not a sharp font anymore. The problem I'm experiencing is I'm using a special TTF Barcode font that creates something a scanner can read. When I flip the barcode font, the fuzziness isn't good since the barcode lines are so close together and the scanner cannot read it. Here's the code:
Function LoadImage(ByVal sImageText as String, iRotationAngle as Integer, ByVal sFontName as String, iFontSize as Integer)
Dim bmpImage As New Drawing.Bitmap(1, 1)
Dim iWidth As Integer = 0
Dim iHeight As Integer = 0
'// Create the Font object for the image text drawing.
Dim MyFont As New Drawing.Font(sFontName, iFontSize) ', System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point)
'// Create a graphics object to measure the text's width and height.
Dim MyGraphics As Drawing.Graphics = Drawing.Graphics.FromImage(bmpImage)
'// This is where the bitmap size is determined.
iWidth = MyGraphics.MeasureString(sImageText, MyFont).Width
iHeight = MyGraphics.MeasureString(sImageText, MyFont).Height
'// Create the bmpImage again with the correct size for the text and font.
bmpImage = New Drawing.Bitmap(bmpImage, New Drawing.Size(iWidth, iHeight))
'// Add the colors to the new bitmap.
MyGraphics = Drawing.Graphics.FromImage(bmpImage)
MyGraphics.Clear(Drawing.Color.White)
MyGraphics.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
MyGraphics.TranslateTransform(iWidth,iHeight)
MyGraphics.RotateTransform(iRotationAngle)
MyGraphics.DrawString(sImageText, MyFont, New Drawing.SolidBrush(Drawing.Color.Black), 0, 0)
MyGraphics.Flush()
Dim stream As IO.MemoryStream = New IO.MemoryStream
Dim bitmapBytes As Byte()
'Create bitmap
bmpImage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp)
bitmapBytes = stream.ToArray
stream.Close()
bmpImage.Dispose()
Return bitmapBytes
End Function
I really don't know why there's not a built-in way to just flip text. It'll let me reverse it left-to-right. Ridiculous.
Thanks
Related
Looking for some much appreciated help. I'm trying to draw a smaller bitmap into a larger bitmap. I'm trying to use Lockbits to make the process go faster. But my .DrawImage function will not execute. The error is a general GDI+ error (Unhandled Exception). This is my code snippet. What am i doing wrong? The Misc.Lockbitmap is verified to work so i don't think the error is there, but I can put that code up here as well if that helps.
I should add that this is part of a much bigger code. To clarify: I am able to use execute the line with .DrawImage if i disable the lockbits.
Dim largerFile As New Bitmap(BitMapSizeX, BitMapSizeY)
Call MiscClass.LockBitmap(largerFile, PixBytes, RowSizeBytes)
Dim GraphicsModifier As Graphics = Graphics.FromImage(largerFile)
Dim currentPic As New Bitmap(smallerFilePath.Tostring) ' & ".jpg")
GraphicsModifier.DrawImage(currentPic, picAndLoc.XLoc, picAndLoc.YLoc, ComponentSize, ComponentSize)
Public Shared m_BitmapData As BitmapData
' Lock the bitmap's data.
Public Shared Sub LockBitmap(ByRef bm As Bitmap, ByRef PixBytes() As Byte, ByRef RowSizeBytes As Integer)
Dim bounds As Rectangle = New Rectangle(0, 0, bm.Width, bm.Height)
m_BitmapData = bm.LockBits(bounds, Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format24bppRgb)
RowSizeBytes = m_BitmapData.Stride
' Allocate room for the data.
Dim total_size As Integer = m_BitmapData.Stride * m_BitmapData.Height
ReDim PixBytes(total_size)
Marshal.Copy(m_BitmapData.Scan0, PixBytes, 0, total_size)
End Sub
Say, i have a PictureBox1 in my Form. That PictureBox have an Image in it, and I want to rotate it. Actually, I've learn how to rotate the image by 90,180,270.... degree. But how can I rotate it by 20 degrees or 45 degrees?
This is what I've learn
PictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
My solution:
Public Function RotateImage(ByRef image As Image, ByVal offset As PointF, ByVal angle As Decimal) As Bitmap
If image Is Nothing Then
Throw New ArgumentNullException("image")
End If
''create a new empty bitmap to hold rotated image
Dim rotatedBmp As Bitmap = New Bitmap(image.Width, image.Height)
'Dim rotatedBmp As Bitmap = New Bitmap(image)
rotatedBmp.SetResolution(image.HorizontalResolution, image.VerticalResolution)
''make a graphics object from the empty bitmap
Dim g As Graphics = Graphics.FromImage(rotatedBmp)
''Put the rotation point in the center of the image
g.TranslateTransform(offset.X, offset.Y)
''rotate the image
g.RotateTransform(angle)
''move the image back
g.TranslateTransform(-offset.X, -offset.Y)
''draw passed in image onto graphics object
'g.DrawImage(image, New PointF(0, 0))
g.DrawImage(image, offset)
Return rotatedBmp
End Function
I am using the function below to convert some ink shapes I have drawn on a picturebox control to Base64
Function GetBase64(ByVal InkCollector As InkCollector) As String
Dim utf8 As UTF8Encoding = New UTF8Encoding()
InkCollector.Enabled = False
Return utf8.GetString(InkCollector.Ink.Save(PersistenceFormat.Base64InkSerializedFormat, CompressionMode.Maximum))
InkCollector.Enabled = True
End Function
I can then use the string to reproduce the sketch into a new image by feeding it to this function:
Function Base64toImage(ByVal Base64 As String) As System.Drawing.Image
Dim utf8 As UTF8Encoding = New UTF8Encoding()
Dim imgSig As System.Drawing.Image
Dim tmploadedInk As Ink = New Ink()
Dim strGIF As String
Dim imageBytes() As Byte
Dim MS As MemoryStream
'Load the Base64 String in Format(PersistenceFormat = Base64InkSerializedFormat) as ink
tmploadedInk.Load(utf8.GetBytes(Base64))
'Convert the ink to Base64 String in format (PersistenceFormat.Gif, CompressionMode.Maximum)
strGIF = Convert.ToBase64String(tmploadedInk.Save(PersistenceFormat.Gif, CompressionMode.Maximum))
'Convert Base64 String to Byte Array
imageBytes = Convert.FromBase64String(strGIF)
MS = New MemoryStream(imageBytes, 0, imageBytes.Length)
' Convert byte[] to Image
MS.Write(imageBytes, 0, imageBytes.Length)
imgSig = Image.FromStream(MS, True)
Return imgSig
End Function
This works fine except for the fact the shapes are cropped and therefore they get aligned to the top left corner of the generated image. If I use a line of code like below:
PictureBox1.Image = Signiture.Base64toImage(Signiture.GetBase64(Sketch_InkCollector))
The shapes appear at the top left and ignore the fact that they might have been drawn at the center of the original picture box. I believe that the positioning data is being properly saved into the Base64 string, but are somehow being ignored when the shapes are transferred to the image variable. The reason I believe this is because when I use the function below with the Base64 string, the shapes get placed properly in their original position.
Sub SetBase64PictureBox(ByVal InkCollector As InkCollector, ByVal PictureBox As PictureBox, ByVal Base64 As String)
Dim loadedInk As Ink = New Ink()
Dim utf8 As UTF8Encoding = New UTF8Encoding()
InkCollector.Enabled = False
InkCollector.Ink.DeleteStrokes() ' Clear all strokes
loadedInk.Load(utf8.GetBytes(Base64))
InkCollector.Ink = loadedInk
InkCollector.Enabled = True
PictureBox.Invalidate()
End Sub
Thank you
Update:
Found a temporary solution in this link:
InkPicture control - How to save both ink and image to an image file
It is not exactly what I was after but the end result is the same since I wanted to the combined picture and the ink strokes into an image to show in a PDF file.
I use VB.net
I use ListView to view 50*x thumbnails(.jpg) at the left to the items.
But instead of 32bit high quality thumbnails, ListView lowers them to 8bit or 16bit(not sure).
Here's the code
Private Sub afterprocessed()
ListView1.Items.Clear()
Dim imlTemp As New ImageList
Dim dirFiles() As String = IO.Directory.GetFiles("backend\communicate\thumbnails")
Dim _imgList As New ImageList
Dim imgSize As New Size
imgSize.Width = 50
ListView1.SmallImageList = _imgList
Dim count As Integer = 0
Dim item As New ListViewItem
For Each dirFile As String In dirFiles
Dim imgFilename As String = IO.Path.GetFileNameWithoutExtension(dirFile)
Dim img As New System.Drawing.Bitmap(dirFile)
Dim imgImage As Image = Image.FromFile(dirFile)
'Dim imgHeight As Integer
'imgHeight = imgImage.Height
imgSize.Height = imgImage.Height
_imgList.ImageSize = imgSize
_imgList.Images.Add(img.Clone)
ListView1.Items.Add(imgFilename, count)
count += 1
Next
End Sub
And the quality is noticably low. Compared to original JPG
Any advice? I'd greatly appreciate it :D
You need this.
this.ListView1.SmallImageList.ColorDepth = ColorDepth.Depth32Bit;
I had a similar problem one time and I found out it was being caused by using the ImageList.ImageSize method, which seems to use a poor resizing algorithm. I resolved it by ensuring all the images I used were already the correct size on disk before loading them. But if that's not an option for you it shouldn't be too difficult to resize them before adding them to the ImageList.
I need to display a table in rich text within a form window. It is only a two column table, so tabbing works fine to line everything up (since RichTextBox does not support RTF tables). However, sometimes the tab stops are incorrect because of non-fixed width fonts.
So, I need a way to measure the pixel width of a particular string with a particular font (Arial 10) and space or tab pad to make sure that everything aligns.
I know about the Graphics.MeaureString method, but since this is in a rich text box, I don't have an initilzed PaintEventArgs variable, and that would seem like overkill to create JUST to measure one string.
From MSDN:
Private Sub MeasureStringMin(ByVal e As PaintEventArgs)
' Set up string.
Dim measureString As String = "Measure String"
Dim stringFont As New Font("Arial", 16)
' Measure string.
Dim stringSize As New SizeF
stringSize = e.Graphics.MeasureString(measureString, stringFont)
' Draw rectangle representing size of string.
e.Graphics.DrawRectangle(New Pen(Color.Red, 1), 0.0F, 0.0F, _
stringSize.Width, stringSize.Height)
' Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
New PointF(0, 0))
End Sub
So is the best bet just to create a dummy PaintEventArgs instance? If so, what is the best way to do that (since I'll have to call this string measuring method several hundred times)?
Also, I don't really want to have to use a fixed width font - they just don't look as good.
Use this
Dim g as Graphics = richbox.CreateGraphics()
Dim sz as SizeF = g.MeasureString(...)