Answered by:
How to rotate an image in SSRS

Question
-
I am building a report in SSRS using SQL where I an converting a barcode into an image using the code below, this code was taken from here. I am not a
VB.Net
developer but this code works perfectly for me. The issue is that the layout of my report is with a vertical barcode image rather than horizontal, and I don't see any option where I can rotate the image OOB. Can anyone help me here to rotate the image using the below code.Public Shared Function GenerateImage(ByVal fontName As String, ByVal stringText As String) As Byte() Dim oGraphics As System.Drawing.Graphics Dim barcodeSize As System.Drawing.SizeF Dim ms As System.IO.MemoryStream Dim i As System.Drawing.Image Using font As New System.Drawing.Font(New System.Drawing.FontFamily(fontName), 36) Using tmpBitmap As New System.Drawing.Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb) oGraphics = System.Drawing.Graphics.FromImage(tmpBitmap) oGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel barcodeSize = oGraphics.MeasureString(stringText, font) oGraphics.Dispose() End Using Using newBitmap As New System.Drawing.Bitmap(barcodeSize.Width, barcodeSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb) oGraphics = System.Drawing.Graphics.FromImage(newBitmap) oGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel Using oSolidBrushWhite As New System.Drawing.SolidBrush(System.Drawing.Color.White) Using oSolidBrushBlack As New System.Drawing.SolidBrush(System.Drawing.Color.Black) oGraphics.FillRectangle(oSolidBrushWhite, New System.Drawing.Rectangle(0, 0, barcodeSize.Width, barcodeSize.Height)) oGraphics.DrawString(stringText, font, oSolidBrushBlack, 0, 0) End Using End Using ms = New System.IO.MemoryStream() newBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png) End Using
This report is for CRM Online.
- Edited by hkhan Sunday, June 15, 2014 5:13 PM
Sunday, June 15, 2014 5:04 PM
Answers
-
Hi,
first of all thanks for sharing the link, very interesting.
I didn't test what I'm saying but you can try if works.The code is using SystemDrawing namespace, this means that probably we can use also other functions coming from that namespace.
Before you save the bitmap to the memory stream (in the code you post some lines end line are missing) you can try to rotate the image using this code:newBitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone)
so the code will be:
newBitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone) ms = New System.IO.MemoryStream() newBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
I choosed 270 because the function value is clockwise, so 270 is equal to 90 anticlockwise.
My blog: www.crmanswers.net - Rockstar 365 Profile
- Marked as answer by hkhan Monday, June 16, 2014 8:39 AM
- Edited by Guido PreiteMVP Monday, June 16, 2014 9:04 AM updated namespace
Sunday, June 15, 2014 8:51 PM
All replies
-
Hi,
first of all thanks for sharing the link, very interesting.
I didn't test what I'm saying but you can try if works.The code is using SystemDrawing namespace, this means that probably we can use also other functions coming from that namespace.
Before you save the bitmap to the memory stream (in the code you post some lines end line are missing) you can try to rotate the image using this code:newBitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone)
so the code will be:
newBitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone) ms = New System.IO.MemoryStream() newBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
I choosed 270 because the function value is clockwise, so 270 is equal to 90 anticlockwise.
My blog: www.crmanswers.net - Rockstar 365 Profile
- Marked as answer by hkhan Monday, June 16, 2014 8:39 AM
- Edited by Guido PreiteMVP Monday, June 16, 2014 9:04 AM updated namespace
Sunday, June 15, 2014 8:51 PM -
When I add
newBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone)
like you said and run the report, it gives me the following error Name RotateFlipType is not declared.
I have already imported System.Drawing assembly reference in SSRS.
- Edited by hkhan Monday, June 16, 2014 8:02 AM
Monday, June 16, 2014 7:42 AM -
Apparently custom code in SSRS reports do not allow Imports statement. So it should be like this
newBitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone)
Monday, June 16, 2014 8:39 AM -
Yes, you are correct with the import, so it works now right?
My blog: www.crmanswers.net - Rockstar 365 Profile
Monday, June 16, 2014 9:03 AM -
It does Sir.Monday, June 16, 2014 9:07 AM