html中文本框角度旋转,HTML表格中的垂直(旋转)文本

经过两个多小时的尝试后,我可以肯定地说到目前为止提到的所有方法都无法在浏览器中运行,或者甚至跨浏览器版本也无法运行...

例如(top upvoted答案):

filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=0.083); /* IE6,IE7 */

-ms-filter: "progid:DXImageTransform.Microsoft.BasicImage(rotation=0.083)"; /* IE8 */

在IE9中旋转两次,一次用于过滤,一次用于-ms-filter,所以......

所有其他提到的方法也不起作用,至少在你不必设置表头单元格的固定高度/宽度(带背景颜色)时,它应该自动调整到最高元素的大小。

所以详细说明Nathan Long提出的服务器端图像生成,这是唯一普遍使用的方法,这里是我用于通用处理程序的VB.NET代码(* .ashx):

Imports System.Web

Imports System.Web.Services

Public Class GenerateImage

Implements System.Web.IHttpHandler

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

'context.Response.ContentType = "text/plain"

'context.Response.Write("Hello World!")

context.Response.ContentType = "image/png"

Dim strText As String = context.Request.QueryString("text")

Dim strRotate As String = context.Request.QueryString("rotate")

Dim strBGcolor As String = context.Request.QueryString("bgcolor")

Dim bRotate As Boolean = True

If String.IsNullOrEmpty(strText) Then

strText = "No Text"

End If

Try

If Not String.IsNullOrEmpty(strRotate) Then

bRotate = System.Convert.ToBoolean(strRotate)

End If

Catch ex As Exception

End Try

'Dim img As System.Drawing.Image = GenerateImage(strText, "Arial", bRotate)

'Dim img As System.Drawing.Image = CreateBitmapImage(strText, bRotate)

' Generic error in GDI+

'img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png)

'Dim bm As System.Drawing.Bitmap = New System.Drawing.Bitmap(img)

'bm.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png)

Using msTempOutputStream As New System.IO.MemoryStream

'Dim img As System.Drawing.Image = GenerateImage(strText, "Arial", bRotate)

Using img As System.Drawing.Image = CreateBitmapImage(strText, bRotate, strBGcolor)

img.Save(msTempOutputStream, System.Drawing.Imaging.ImageFormat.Png)

msTempOutputStream.Flush()

context.Response.Buffer = True

context.Response.ContentType = "image/png"

context.Response.BinaryWrite(msTempOutputStream.ToArray())

End Using ' img

End Using ' msTempOutputStream

End Sub ' ProcessRequest

Private Function CreateBitmapImage(strImageText As String) As System.Drawing.Image

Return CreateBitmapImage(strImageText, True)

End Function ' CreateBitmapImage

Private Function CreateBitmapImage(strImageText As String, bRotate As Boolean) As System.Drawing.Image

Return CreateBitmapImage(strImageText, bRotate, Nothing)

End Function

Private Function InvertMeAColour(ColourToInvert As System.Drawing.Color) As System.Drawing.Color

Const RGBMAX As Integer = 255

Return System.Drawing.Color.FromArgb(RGBMAX - ColourToInvert.R, RGBMAX - ColourToInvert.G, RGBMAX - ColourToInvert.B)

End Function

Private Function CreateBitmapImage(strImageText As String, bRotate As Boolean, strBackgroundColor As String) As System.Drawing.Image

Dim bmpEndImage As System.Drawing.Bitmap = Nothing

If String.IsNullOrEmpty(strBackgroundColor) Then

strBackgroundColor = "#E0E0E0"

End If

Dim intWidth As Integer = 0

Dim intHeight As Integer = 0

Dim bgColor As System.Drawing.Color = System.Drawing.Color.LemonChiffon ' LightGray

bgColor = System.Drawing.ColorTranslator.FromHtml(strBackgroundColor)

Dim TextColor As System.Drawing.Color = System.Drawing.Color.Black

TextColor = InvertMeAColour(bgColor)

'TextColor = Color.FromArgb(102, 102, 102)

' Create the Font object for the image text drawing.

Using fntThisFont As New System.Drawing.Font("Arial", 11, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel)

' Create a graphics object to measure the text's width and height.

Using bmpInitialImage As New System.Drawing.Bitmap(1, 1)

Using gStringMeasureGraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmpInitialImage)

' This is where the bitmap size is determined.

intWidth = CInt(gStringMeasureGraphics.MeasureString(strImageText, fntThisFont).Width)

intHeight = CInt(gStringMeasureGraphics.MeasureString(strImageText, fntThisFont).Height)

' Create the bmpImage again with the correct size for the text and font.

bmpEndImage = New System.Drawing.Bitmap(bmpInitialImage, New System.Drawing.Size(intWidth, intHeight))

' Add the colors to the new bitmap.

Using gNewGraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmpEndImage)

' Set Background color

'gNewGraphics.Clear(Color.White)

gNewGraphics.Clear(bgColor)

gNewGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias

gNewGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias

''''

'gNewGraphics.TranslateTransform(bmpEndImage.Width, bmpEndImage.Height)

'gNewGraphics.RotateTransform(180)

'gNewGraphics.RotateTransform(0)

'gNewGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault

gNewGraphics.DrawString(strImageText, fntThisFont, New System.Drawing.SolidBrush(TextColor), 0, 0)

gNewGraphics.Flush()

If bRotate Then

'bmpEndImage = rotateImage(bmpEndImage, 90)

'bmpEndImage = RotateImage(bmpEndImage, New PointF(0, 0), 90)

'bmpEndImage.RotateFlip(RotateFlipType.Rotate90FlipNone)

bmpEndImage.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone)

End If ' bRotate

End Using ' gNewGraphics

End Using ' gStringMeasureGraphics

End Using ' bmpInitialImage

End Using ' fntThisFont

Return bmpEndImage

End Function ' CreateBitmapImage

' http://msdn.microsoft.com/en-us/library/3zxbwxch.aspx

' http://msdn.microsoft.com/en-us/library/7e1w5dhw.aspx

' http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=286

' http://road-blogs.blogspot.com/2011/01/rotate-text-in-ssrs.html

Public Shared Function GenerateImage_CrappyOldReportingServiceVariant(ByVal strText As String, ByVal strFont As String, bRotate As Boolean) As System.Drawing.Image

Dim bgColor As System.Drawing.Color = System.Drawing.Color.LemonChiffon ' LightGray

bgColor = System.Drawing.ColorTranslator.FromHtml("#E0E0E0")

Dim TextColor As System.Drawing.Color = System.Drawing.Color.Black

'TextColor = System.Drawing.Color.FromArgb(255, 0, 0, 255)

If String.IsNullOrEmpty(strFont) Then

strFont = "Arial"

Else

If strFont.Trim().Equals(String.Empty) Then

strFont = "Arial"

End If

End If

'Dim fsFontStyle As System.Drawing.FontStyle = System.Drawing.FontStyle.Regular

Dim fsFontStyle As System.Drawing.FontStyle = System.Drawing.FontStyle.Bold

Dim fontFamily As New System.Drawing.FontFamily(strFont)

Dim iFontSize As Integer = 8 '//Change this as needed

' vice-versa, because 270° turn

'Dim height As Double = 2.25

Dim height As Double = 4

Dim width As Double = 1

' width = 10

' height = 10

Dim bmpImage As New System.Drawing.Bitmap(1, 1)

Dim iHeight As Integer = CInt(height * 0.393700787 * bmpImage.VerticalResolution) 'y DPI

Dim iWidth As Integer = CInt(width * 0.393700787 * bmpImage.HorizontalResolution) 'x DPI

bmpImage = New System.Drawing.Bitmap(bmpImage, New System.Drawing.Size(iWidth, iHeight))

'// Create the Font object for the image text drawing.

'Dim MyFont As New System.Drawing.Font("Arial", iFontSize, fsFontStyle, System.Drawing.GraphicsUnit.Point)

'// Create a graphics object to measure the text's width and height.

Dim MyGraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmpImage)

MyGraphics.Clear(bgColor)

Dim stringFormat As New System.Drawing.StringFormat()

stringFormat.FormatFlags = System.Drawing.StringFormatFlags.DirectionVertical

'stringFormat.FormatFlags = System.Drawing.StringFormatFlags.DirectionVertical Or System.Drawing.StringFormatFlags.DirectionRightToLeft

Dim solidBrush As New System.Drawing.SolidBrush(TextColor)

Dim pointF As New System.Drawing.PointF(CSng(iWidth / 2 - iFontSize / 2 - 2), 5)

Dim font As New System.Drawing.Font(fontFamily, iFontSize, fsFontStyle, System.Drawing.GraphicsUnit.Point)

MyGraphics.TranslateTransform(bmpImage.Width, bmpImage.Height)

MyGraphics.RotateTransform(180)

MyGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault

MyGraphics.DrawString(strText, font, solidBrush, pointF, stringFormat)

MyGraphics.ResetTransform()

MyGraphics.Flush()

'If Not bRotate Then

'bmpImage.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone)

'End If

Return bmpImage

End Function ' GenerateImage

ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable

Get

Return False

End Get

End Property ' IsReusable

End Class

请注意,如果您认为这部分

Using msTempOutputStream As New System.IO.MemoryStream

'Dim img As System.Drawing.Image = GenerateImage(strText, "Arial", bRotate)

Using img As System.Drawing.Image = CreateBitmapImage(strText, bRotate, strBGcolor)

img.Save(msTempOutputStream, System.Drawing.Imaging.ImageFormat.Png)

msTempOutputStream.Flush()

context.Response.Buffer = True

context.Response.ContentType = "image/png"

context.Response.BinaryWrite(msTempOutputStream.ToArray())

End Using ' img

End Using ' msTempOutputStream

可以替换为

img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png)

因为它可以在开发服务器上运行,所以如果将它部署到Windows 2003 IIS 6服务器上,那么你会非常错误地认为相同的代码不会抛出Generic GDI +异常......

然后像这样使用它:

bla

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值