Нужна помощь для преобразования VB-кода в C # - PullRequest
0 голосов
/ 12 мая 2011

Я хочу обрезать изображение, полученное из базы данных.Я нашел следующий код в сети, но он находится в VB.Я делаю веб-страницу на основе asp.net c # и понятия не имею о vb

Может ли кто-нибудь постараться дать мне код C # для следующего vb-кода?

помощь очень полезна иЯ был бы очень благодарен вам.

Вот код:

  Dim ImageURL As String = "/Uploads/Gallery/" + ImageID + ".jpg"

        Dim oBitmap As New Bitmap(Server.MapPath(ImageURL))

        Dim Ratio As Integer = oBitmap.Width / 572


        Try

            Dim X1, Y1, X2, Y2, Width, Height As Integer

            Try
                X1 = x1txt.Text
                Y1 = y1txt.Text
                X2 = x2txt.Text
                Y2 = y2txt.Text
                Width = widthtxt.Text
                Height = heighttxt.Text
            Catch
            End Try

            X1 = X1 * Ratio
            Y1 = Y1 * Ratio
            X2 = X2 * Ratio
            Y2 = Y2 * Ratio
            Width = Width * Ratio
            Height = Height * Ratio

            Dim TopLeft As New Point(X1, Y1)
            Dim BottomRight As New Point(CInt(X1) + CInt(Height), CInt(Y1) + CInt(Width))

            Dim BitmapToSave As New Bitmap(oBitmap.Width, oBitmap.Height)

            Dim objGraphics As System.Drawing.Graphics
            objGraphics = System.Drawing.Graphics.FromImage(BitmapToSave)
            objGraphics.DrawImage(oBitmap, 0, 0)

            oBitmap.Dispose()

            BitmapToSave = img.CropImage(BitmapToSave, TopLeft, BottomRight)

            BitmapToSave.Save(Server.MapPath(ImageURL))

            BitmapToSave.Dispose()

        Catch
        End Try

Ответы [ 3 ]

2 голосов
/ 12 мая 2011

Попробуйте этот инструмент, чтобы сделать такие вещи:

Я считаю, что StackOverflow - это не сообщество "сделай сам"!

2 голосов
/ 12 мая 2011

Это моя версия, сделанная от руки и C # -orized.Надеюсь, это не опечатки.Я изменил предложение try, так как оно могло иметь нулевую ширину и высоту.

string imageUrl = "/Uploads/Gallery/" + ImageID + ".jpg";
Bitmap bitmap = new Bitmap(Server.MapPath(imageUrl));
int ratio = bitmap.Width / 572;
int x1,y1,x2,y2,width,height;

try
{
    x1 = int.Parse(x1txt.Text);
    y1 = int.Parse(y1txt.Text);
    x2 = int.Parse(x2text.Text);
    width = int.Parse(widthtxt.Text);
    height = int.Parse(heightxt.Text);

    x1 *= ratio;
    y1 *= ratio;
    x2 *= ratio;
    y2 *= ratio;
    width *= ratio;

    Point topLeft = new Point(x1,y1);
    Point bottomRight = new Point(x1 + height,y1 + width);
    Bitmap bitmapToSave = new Bitmap(bitmap.Width,bitmap.Height);

    using (Graphics graphics = Graphics.FromImage(bitmapToSave))
    {
        graphics.DrawImage(bitmap,0,0);
    }

    bitmapToSave = img.CropImage(bitmapToSave,topLeft,bottomRight);
    bitmapToSave.Save(Server.MapPath(imageUrl));
}
catch (FormatException e)
{
    // Log
}
1 голос
/ 12 мая 2011

Вы можете использовать этот конвертер: Конвертировать VB.NET в C #

string ImageURL = "/Uploads/Gallery/" + ImageID + ".jpg";

Bitmap oBitmap = new Bitmap(Server.MapPath(ImageURL));

int Ratio = oBitmap.Width / 572;



try {
    int X1 = 0;
    int Y1 = 0;
    int X2 = 0;
    int Y2 = 0;
    int Width = 0;
    int Height = 0;

    try {
        X1 = x1txt.Text;
        Y1 = y1txt.Text;
        X2 = x2txt.Text;
        Y2 = y2txt.Text;
        Width = widthtxt.Text;
        Height = heighttxt.Text;
    } catch {
    }

    X1 = X1 * Ratio;
    Y1 = Y1 * Ratio;
    X2 = X2 * Ratio;
    Y2 = Y2 * Ratio;
    Width = Width * Ratio;
    Height = Height * Ratio;

    Point TopLeft = new Point(X1, Y1);
    Point BottomRight = new Point(Convert.ToInt32(X1) + Convert.ToInt32(Height), Convert.ToInt32(Y1) + Convert.ToInt32(Width));

    Bitmap BitmapToSave = new Bitmap(oBitmap.Width, oBitmap.Height);

    System.Drawing.Graphics objGraphics = null;
    objGraphics = System.Drawing.Graphics.FromImage(BitmapToSave);
    objGraphics.DrawImage(oBitmap, 0, 0);

    oBitmap.Dispose();

    BitmapToSave = img.CropImage(BitmapToSave, TopLeft, BottomRight);

    BitmapToSave.Save(Server.MapPath(ImageURL));

    BitmapToSave.Dispose();

} catch {
}
...