需要帮助将vb代码转换为c#

需要帮助将vb代码转换为c#

问题描述:

我想裁剪从数据库中检索的图像。我在网上找到了下面的代码,但它在vb中。我正在做基于asp.net c#的网页,我不知道vb需要帮助将vb代码转换为c#

任何人都可以痛苦给我的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 
+1

使用代码转换器:) – Anuraj 2011-05-12 07:14:52

+1

还有DeveloperFusion一个别人都挂牌,有一个Telerik的一个:http://converter.telerik.com/ – 2011-05-12 07:44:47

您可以使用该转换器:Convert VB.NET to 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 { 
} 
+0

大概永远不会尽管编译,为x1txt。文字等将永远是一个字符串(不是我说的不使用转换器,但他们喜欢谷歌翻译了很多时间) – 2011-05-12 07:31:27

+1

但我相信转换代码并稍后纠正它比手动翻译它更容易如果你不知道源语言。 – 2011-05-12 07:34:43

尝试使用该工具做这样的事情:

我相信StackOverflow不是一个为你工作的社区!

这是我的版本,由手工和C#完成。希望它没有错别字。我已经改变了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 
} 
+0

+ +1为此而努力。 – 2011-05-12 07:43:34