Martin Muldoon Martin Muldoon - 3 months ago 15
C# Question

Issue porting VB.Net code to C#

I am trying to port code using various tools and I keep getting an error indicating that on line 59 there should be an end statement. Can anyone convert the following?

Imports System.IO
Imports System.Drawing

Public Class Handler : Implements IHttpHandler

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

Dim result As String = String.Empty

If context.Request.Files.Count > 0 Then
result = "File Processed"
Dim files As HttpFileCollection = context.Request.Files

For i As Integer = 0 To files.Count - 1
Dim file As HttpPostedFile = files(i)
Dim fname As String
Dim fpath As String
Dim saveAsPath As String

fname = file.FileName

'Save Large Image
fpath = context.Server.MapPath("~/Images/stock/large/")
saveAsPath = Path.Combine(fpath, fname)
file.SaveAs(saveAsPath)

'Save thumbnail
Dim thumbimage As Bitmap
Dim originalimage As Bitmap
Dim width As Integer = 200
Dim height As Integer = 200
Dim newwidth As Integer
Dim newheight As Integer

originalimage = Image.FromFile(saveAsPath) 'Get the file that was just saved
If originalimage.Width > originalimage.Height Then
newheight = originalimage.Height / originalimage.Width * height
newwidth = width
Else
newheight = height
newwidth = originalimage.Width / originalimage.Height * width
End If

thumbimage = New Bitmap(newwidth, newheight)

Dim gr As Graphics = Graphics.FromImage(thumbimage)
gr.DrawImage(originalimage, 0, 0, newwidth, newheight)
thumbimage.Save(context.Server.MapPath("~/Images/stock" + "\" + Path.GetFileName(saveAsPath)), Imaging.ImageFormat.Jpeg)

Next
Else
result = "No File Detected!"

End If

context.Response.ContentType = "text/plain"
context.Response.Write(result)

End Sub

Answer

You are missing End Class which should be at the end of your code sample.

Here is the converted C#

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Drawing;

public class Handler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string result = string.Empty;

        if (context.Request.Files.Count > 0) {
            result = "File Processed";
            HttpFileCollection files = context.Request.Files;

            for (int i = 0; i <= files.Count - 1; i++) {
            HttpPostedFile file = files(i);
                    string fname = null;
                string fpath = null;
                string saveAsPath = null;

                fname = file.FileName;

                //Save Large Image
                fpath = context.Server.MapPath("~/Images/stock/large/");
                saveAsPath = Path.Combine(fpath, fname);
                file.SaveAs(saveAsPath);

                //Save thumbnail
                Bitmap thumbimage = default(Bitmap);
                Bitmap originalimage = default(Bitmap);
                int width = 200;
                int height = 200;
                int newwidth = 0;
                int newheight = 0;

                originalimage = Image.FromFile(saveAsPath);
                //Get the file that was just saved
                if (originalimage.Width > originalimage.Height) {
                    newheight = originalimage.Height / originalimage.Width * height;
                    newwidth = width;
                } else {
                    newheight = height;
                    newwidth = originalimage.Width / originalimage.Height * width;
                }

                thumbimage = new Bitmap(newwidth, newheight);

                Graphics gr = Graphics.FromImage(thumbimage);
                gr.DrawImage(originalimage, 0, 0, newwidth, newheight);
                thumbimage.Save(context.Server.MapPath("~/Images/stock" + "\\" + Path.GetFileName(saveAsPath)), Imaging.ImageFormat.Jpeg);

            }
        } else {
            result = "No File Detected!";

        }

        context.Response.ContentType = "text/plain";
        context.Response.Write(result);

    }
}