لماذا أنا على هذا عامة, غير الوصف خطأ في GDI+ عند محاولة حفظ PNG ؟
سؤال
لدي وظيفة بشكل حيوي إضافة نص إلى صورة في المحددة مسبقا الفور.أصلا أنا فعلت هذا مع ملفات jpeg ، وكان العامل.أنا تحولت إلى PNG حتى الصور سيكون أفضل نوعية ، الأصلي jpeg نوع من بيكسلي.على أية حال هنا هو قانون بلدي.فإنه ينفذ إلى أسفل oBitmap.Save()
, ثم يموت مع "جنرال حدث خطأ في GDI+".
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "image/png"
context.Response.Clear()
context.Response.BufferOutput = True
Try
Dim oText As String = context.Server.HtmlDecode(context.Request.QueryString("t"))
If String.IsNullOrEmpty(oText) Then oText = "Placeholder"
Dim oPType As String = context.Server.HtmlDecode(context.Request.QueryString("p"))
If String.IsNullOrEmpty(oPType) Then oPType = "none"
Dim imgPath As String = ""
Select Case oPType
Case "c"
imgPath = "img/banner_green.png"
Case "m"
imgPath = "img/banner_blue.png"
Case Else
Throw New Exception("no ptype")
End Select
Dim oBitmap As Bitmap = New Bitmap(context.Server.MapPath(imgPath))
Dim oGraphic As Graphics = Graphics.FromImage(oBitmap)
Dim frontColorBrush As New SolidBrush(Color.White)
Dim oFont As New Font(FONT_NAME, 30)
Dim oInfo() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders
Dim oEncoderParams As New EncoderParameters(2)
Dim xOffset As Single = Math.Round((oBitmap.Height - oFont.Height) / 2, MidpointRounding.ToEven)
Dim oPoint As New PointF(275.0F, xOffset + 10)
oEncoderParams.Param(0) = New EncoderParameter(Encoder.Quality, 100L)
oEncoderParams.Param(1) = New EncoderParameter(Encoder.ColorDepth,8L)
oGraphic.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
oGraphic.DrawString(oText, oFont, frontColorBrush, oPoint)
oBitmap.Save(context.Response.OutputStream, oInfo(4), oEncoderParams)
context.Response.Output.Write(oBitmap)
oFont.Dispose()
oGraphic.Dispose()
oBitmap.Dispose()
context.Response.Flush()
Catch ex As Exception
End Try
End Sub
فقط التغييرات التي قمت بها على هذا من jpeg الإصدار هي:
context.Response.ContentType = "image/jpeg"
تغيير"image/png"
- تغيير قاعدة الصور (
img/banner_green.jpg
,img/banner_blue.jpg
) إلى.png
- وأضاف الثاني ترميز المعلمة تحديد عمق اللون
- تغيرت
oInfo(1)
(jpeg) إلىoInfo(4)
(png)
هل هناك المزيد من الأشياء التي كنت بحاجة إلى قرص للحصول على هذا الروتين بشكل صحيح توليد PNG ؟
المحلول
وفقا هذا المنصب, الصورة النقطية.حفظ يتطلب seekable تيار حفظ بابوا نيو غينيا ، HttpResponse.OutputStream لا.يجب عليك حفظ الصورة في MemoryStream أولا ثم نسخ محتويات ذلك إلى الاستجابة.OutputStream, مثل:
Dim tempStream as New MemoryStream
oBitmap.Save(tempStream, ImageFormat.Png, oEncoderParams)
Response.OutputStream.Write(tempStream.ToArray(), 0, tempStream.Length)
نلاحظ أيضا أن خط
context.Response.Output.Write(oBitmap)
لا شيء مختلف ثم ماذا كنت تتوقعين. HttpResponse.Output
هو TextWriter ، والزائد كنت تستخدم هنا ، TextWriter.Write(object)
فقط اتصل ToString على الكائن ثم كتابة النتائج في تيار ما في هذه الحالة النتائج في كتابة "النظام.الرسم.نقطية" إلى الإخراج.
نصائح أخرى
أنت التخلص من الصورة النقطية قبل التنظيف الرد ؟ محاولة التقليب التي حولها.أيضا, يبدو أنك الكتابة النقطية إلى تيار مرتين.لست متأكدا لماذا كنت تفعل ذلك.حفظ صورة نقطية إلى دفق إخراج أو استخدام أسلوب الكتابة من وجوه الرد ، ولكن ليس على حد سواء.