hi to all....

actually i m facing a problem..i had convert the vb to c# code using http://www.carlosag.net/Tools/CodeTranslator/

wen i try run it ..i get the error...


here i copied the small part of VB code...that i face the error

Private Sub GenerateImage()

        ' Create a Bitmap Object
        Dim xBmp As Bitmap = New Bitmap(m_Width, m_Height, PixelFormat.Format32bppArgb)

        ' Create a Graphics Object from the Bitmap Object
        Dim xGraf As Graphics = Graphics.FromImage(xBmp)

        ' Set the Graphics Object Smoothing Mode
        xGraf.SmoothingMode = SmoothingMode.HighQuality

        ' Create the Rectangle Object
        Dim xRect As Rectangle = New Rectangle(0, 0, m_Width, m_Height)

        ' Create  the Hatch Brush used for the Background 
        ' and Fill it in the Graphics Object
        Dim hatchBrush As HatchBrush = New HatchBrush(m_BackGroundType, m_BackGroundSubColor, m_BackGroundColor)
        xGraf.FillRectangle(hatchBrush, xRect)

        ' Set up the Captcha Text Font.
        Dim size As SizeF
        Dim fontSize As Single = xRect.Height + 1
        Dim font As Font

        ' Adjust the font size until the text fits within the image.
        Do
            fontSize = fontSize - 1
            font = New Font(m_FontFace, fontSize, FontStyle.Bold)
            size = xGraf.MeasureString(m_Text, font)
        Loop While size.Width > xRect.Width

        ' Set up the text format.
        Dim format As StringFormat = New StringFormat()
        format.Alignment = StringAlignment.Center
        format.LineAlignment = StringAlignment.Center

        ' Create a path using the text and warp it randomly.
        Dim path As GraphicsPath = New GraphicsPath()
        path.AddString(m_Text, font.FontFamily, CType(font.Style, Integer), font.Size, xRect, format)

        Dim Points() As PointF = { _
            New PointF(m_Random.Next(xRect.Width) / 4.0F, m_Random.Next(xRect.Height) / 4.0F), _
            New PointF(xRect.Width - m_Random.Next(xRect.Width) / 4.0F, m_Random.Next(xRect.Height) / 4.0F), _
            New PointF(m_Random.Next(xRect.Width) / 4.0F, xRect.Height - m_Random.Next(xRect.Height) / 4.0F), _
            New PointF(xRect.Width - m_Random.Next(xRect.Width) / 4.0F, xRect.Height - m_Random.Next(xRect.Height) / 4.0F) _
                        }

        Dim matrix As Matrix = New Matrix()
        matrix.Translate(0.0F, 0.0F)

        path.Warp(Points, xRect, matrix, WarpMode.Perspective, 0.0F)

        xGraf.DrawPath(New Pen(m_ForeColor), path)

        ' Clean up.
        font.Dispose()
        hatchBrush.Dispose()
        xGraf.Dispose()

        ' Set the image.
        m_Image = xBmp

    End Sub

#End Region ' End Public Methods

here C# code based on above code

private void GenerateImage() {
        //  Create a Bitmap Object
        //Bitmap xBmp = new Bitmap(m_Width, m_Height, PixelFormat.Format32bppArgb);
        Bitmap xBmp = new Bitmap(PixelFormat.Format32bppArgb,n_Width, m_Height);        //  Create a Graphics Object from the Bitmap Object
        Graphics xGraf = Graphics.FromImage(xBmp);
        //  Set the Graphics Object Smoothing Mode
        xGraf.SmoothingMode = SmoothingMode.HighQuality;
        //  Create the Rectangle Object
        Rectangle xRect = new Rectangle(0, 0, n_Width, m_Height);
        //  Create  the Hatch Brush used for the Background 
        //  and Fill it in the Graphics Object
        HatchBrush hatchBrush = new HatchBrush(m_BackGroundType, m_BackGroundSubColor, m_BackGroundColor);
        xGraf.FillRectangle(hatchBrush, xRect);
        //  Set up the Captcha Text Font.
        SizeF size;
        float fontSize = (xRect.Height + 1);
        Font font;
        //  Adjust the font size until the text fits within the image.
        for (
        ; (size.Width > xRect.Width); 
        ) {
            fontSize = (fontSize - 1);
            font = new Font(m_FontFace, fontSize, FontStyle.Bold);
            size = xGraf.MeasureString(m_Text, font);
        }
        //  Set up the text format.
        StringFormat format = new StringFormat();
        format.Alignment = StringAlignment.Center;
        format.LineAlignment = StringAlignment.Center;
        //  Create a path using the text and warp it randomly.
        GraphicsPath path = new GraphicsPath();
        path.AddString(m_Text, font.FontFamily, ((int)(font.Style)), font.Size, xRect, format);
        PointF[] Points;
        new PointF(m_Random.Next(xRect.Width) / 4.0F, m_Random.Next(xRect.Height) / 4.0F);
        new PointF(xRect.Width - m_Random.Next(xRect.Width) / 4.0F, m_Random.Next(xRect.Height) / 4.0F);
        new PointF(m_Random.Next(xRect.Width) / 4.0F, xRect.Height - m_Random.Next(xRect.Height) / 4.0F);
        new PointF(xRect.Width - m_Random.Next(xRect.Width) / 4.0F, xRect.Height - m_Random.Next(xRect.Height) / 4.0F);
        Matrix matrix = new Matrix();
        matrix.Translate(0.0F, 0.0F);
        path.Warp(Points, xRect, matrix, WarpMode.Perspective, 0.0F);
        xGraf.DrawPath(new Pen(m_ForeColor), path);
        //  Clean up.
        font.Dispose();
        hatchBrush.Dispose();
        xGraf.Dispose();
        //  Set the image.
        m_Image = xBmp;
    }

the red highlight is error...
plz help meeeeeee

Dani AI

Generated

The problems here are typical translator mistakes: the Bitmap constructor parameters got rearranged, a measurement variable is used before it is initialized, and the PointF array is never actually filled. is correct to point at the Bitmap call, but there are a few more fixes required before the C# will compile and behave like the VB version.

Do the following (high level):

  • Use the Bitmap constructor overload that takes width, height, then PixelFormat.
  • Pick the font size with a do/while (or initialize size first) so you never evaluate size.Width before it has a value. Stop the loop if the font size reaches a safe minimum to avoid an infinite loop.
  • Create and assign the PointF array in one statement (not by calling new PointF(...) without assigning). Make sure the arithmetic that builds the points uses floating-point division (e.g., divide by 4.0f) so you do not accidentally truncate to int.
  • Dispose graphics objects reliably. Prefer using blocks for Bitmap, Graphics, Brush, Font, Pen so resources are freed even on exceptions.
  • For GraphicsPath.AddString the style parameter must be an integer; casting font.Style is fine. Consider creating the final Font after you determine the size and use GraphicsUnit.Pixel for consistent measurement.

Also double-check you have the proper using directives and references for System.Drawing, System.Drawing.Drawing2D and System.Drawing.Imaging. The MS docs are useful for exact overloads and semantics: see the Bitmap constructors and GraphicsPath.Warp for details (Bitmap constructors, GraphicsPath.Warp). These changes fix the compile errors and prevent common runtime problems like resource leaks and infinite loops.

Try modified statement what you mentioned in red font

Bitmap bm = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            Rectangle rect = new Rectangle(0, 0, Width, Height);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.