GDI Quality problems

smallzoo

Member
Joined
Apr 29, 2008
Messages
8
Programming Experience
3-5
This is really frustrating. I produced some graphs using GDI and vb.net and on the screen they look fine. When you print them off on a black and white printer they look fine BUT on a colour printer the quality is really bad. you can see lines in the colours which is just not acceptable..

I have included the sample code below. I have tried changing to bmp, png etc and it makes not difference.. what am I missing ?.. i can send a copy of the picture produced if anyone can help or you can just copy the code and try running it..

Thanks

Peter


----- Default.aspx
HTML:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>


----- Default.aspx.vb

VB.NET:
Imports System.Configuration
Imports System.Drawing.Printing
Imports System.Drawing
Imports System.Drawing.Imaging

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim brshObj As SolidBrush = New SolidBrush(Color.White)
        Dim new_font As New Font("Tahoma", 6)
        Dim x, y, i As Integer
        Dim MTmonths(12) As String
        Dim iloop, rec, lastx, lasty1, lasty2 As Integer
        Dim starty, endy, ydiv, stepdiv, yaxis As Double
        Dim j

        Dim allMonths As String = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
        Dim monthNames(12) As String
        monthNames = Split(allMonths, ",")

        Dim LY_Yield(12) As Double
        Dim TY_Yield(12) As Double
        Dim TYield As String = "20.42,18,15.6,17.22,17.31,19.14,16.52,18.25,18.52,0,0,0"
        Dim LYield As String = "22.16,20.59,16.72,17.3,18.35,18.24,19.24,19.62,17.62,16.56,14.53,16.08"
        Dim sTYield(13) As String
        Dim sLYield(13) As String
        sTYield = Split(TYield, ",")
        sLYield = Split(LYield, ",")

        For rec = 1 To 12
            LY_Yield(rec) = CDbl(sLYield(rec - 1))
            TY_Yield(rec) = CDbl(sTYield(rec - 1))
            MTmonths(rec) = monthNames(rec - 1)
        Next


        Dim maxy As Double
        Dim miny As Double
        Dim m1, m2 As Double
        maxy = -9999999
        miny = 9999999

        For iloop = 1 To 12
            m1 = TY_Yield(iloop) : m2 = LY_Yield(iloop)
            If m1 <> -9999 And m1 <> 0 Then
                If m1 > maxy Then maxy = m1
                If m1 < miny Then miny = m1
            End If
            If m2 <> -9999 And m2 <> 0 Then
                If m2 > maxy Then maxy = m2
                If m2 < miny Then miny = m2
            End If
        Next

        Dim objBitmap As Bitmap = New Bitmap(340, 230)
        Dim objGraphics As Graphics = Graphics.FromImage(objBitmap)
        Dim bmpNew As Bitmap = New Bitmap(objBitmap.Width, objBitmap.Height)
        'bmpNew.SetResolution(280, 220)
        Dim oGraphic As Graphics = Graphics.FromImage(bmpNew)
        Dim rectObjB As Rectangle = New Rectangle(-50, -50, objBitmap.Width + 50, objBitmap.Height + 50)
        Dim rectObj As Rectangle = New Rectangle(0, 0, objBitmap.Width, objBitmap.Height)
        oGraphic.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
        oGraphic.FillRectangle(brshObj, rectObj)

        Dim pn1 As Pen = New Pen(Color.Red, 1)
        Dim pn2 As Pen = New Pen(Color.RoyalBlue, 3)
        Dim pnax As Pen = New Pen(Color.Black, 2)

        oGraphic.DrawLine(pnax, 20, 210, 340, 210)
        oGraphic.DrawLine(pnax, 20, 210, 20, 20)
        objBitmap.RotateFlip(RotateFlipType.RotateNoneFlipY)
        Dim TextBrsh As Brush = New SolidBrush(Color.Black)
        Dim TextFont As New Font("Arial", 8, FontStyle.Regular)

        For i = 1 To 12
            x = ((i / 12) * 300) - 5
            oGraphic.TextRenderingHint = Text.TextRenderingHint.AntiAliasGridFit
            oGraphic.DrawString(MTmonths(i), TextFont, TextBrsh, x, 210)
        Next


        Dim aPoints(11) As Point
        Dim bPoints(11) As Point

        endy = maxy + (maxy * 20 / 100) : starty = miny - (miny * 20 / 100) : endy = Math.Floor(endy + 0.5) : starty = Math.Floor(starty + 0.5) : stepdiv = Math.Round((endy - starty) / 8, 0)
        ydiv = endy - starty

        For yaxis = starty To endy Step stepdiv
            y = (-1 * ((yaxis - endy) / ydiv * 190))
            oGraphic.DrawLine(Pens.Gray, 20, y + 20, 340, y + 20)
            oGraphic.TextRenderingHint = Text.TextRenderingHint.AntiAliasGridFit
            oGraphic.DrawString(yaxis, TextFont, TextBrsh, 1, y + 10)
        Next

        i = 1 : j = endy : lastx = 20 : lasty1 = 200 : lasty2 = 200

        For iloop = 1 To 12
            x = (i / 12) * 300
            j = LY_Yield(iloop)
            If j <> -9999 Then
                y = (-1 * ((j - endy) / ydiv * 190))
            Else
                y = lasty1
                x = lastx
            End If
            aPoints(i - 1).X = x : aPoints(i - 1).Y = y
            lasty1 = y

            j = TY_Yield(iloop)
            If j <> -9999 Then
                y = (-1 * ((j - endy) / ydiv * 190))
            Else
                y = lasty2
                x = lastx
            End If
            bPoints(i - 1).X = x : bPoints(i - 1).Y = y
            lasty2 = y
            i = i + 1
            lastx = x
        Next


        oGraphic.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias


        ' new compression technique
        Dim Codec, JPEGCodec As System.Drawing.Imaging.ImageCodecInfo
        Dim EncParams As System.Drawing.Imaging.EncoderParameters
        Dim QualitySetting As Long = 100L
        For Each Codec In System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders
            If Codec.MimeType = "image/jpeg" Then
                JPEGCodec = Codec
                Exit For
            End If
        Next
        EncParams = New System.Drawing.Imaging.EncoderParameters(1)
        EncParams.Param(0) = New System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, QualitySetting)
        'MyImage.Save(ImageFileName, JPEGCodec, EncParams)

        'Dim offset As Integer = 1
        'Dim segments As Integer = 3
        'Dim tension As Single = 0.5F
        'oGraphic.DrawCurve(pn1, aPoints, offset, segments, tension)
        'oGraphic.DrawCurve(pn2, bPoints, offset, segments, tension)

        oGraphic.DrawCurve(pn1, aPoints, 0.5F)
        oGraphic.DrawCurve(pn2, bPoints, 0.5F)
        objBitmap = bmpNew
        objBitmap.Save(Server.MapPath("~/GT1.jpg"), ImageFormat.Jpeg)
        objBitmap.Save(Server.MapPath("~/GT2.jpg"), JPEGCodec, EncParams)
        objBitmap.Dispose()


    End Sub
End Class
 
Last edited by a moderator:
Back
Top