Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hey I just started my first C# program and I'd like to use some existing code from my old VB app. But I'm having little problems with the translation. I'm trying to translate this code to Visual C# 2005 Express.

 


   Private Function ScaleImage(ByVal SourceImage As Bitmap, ByVal MaxWidth As Integer, ByVal MaxHeight As Integer) As Bitmap
       Dim OriginalAspectRatio As Double = CDbl(MaxWidth / MaxHeight)
       Dim PicturesAspectRatio As Double = CDbl(SourceImage.Width / SourceImage.Height)
       Dim Picture_Width As Integer
       Dim Picture_Height As Integer

       If OriginalAspectRatio > PicturesAspectRatio Then
           'Skaalataan korkeuden mukaan
           Picture_Height = MaxHeight
           Picture_Width = CInt(PicturesAspectRatio * Picture_Height)
       ElseIf OriginalAspectRatio < PicturesAspectRatio Then
           'Skaalataan leveyden mukaan
           Picture_Width = MaxWidth
           Picture_Height = CInt(Picture_Width / PicturesAspectRatio)
       ElseIf OriginalAspectRatio = PicturesAspectRatio Then
           Picture_Height = MaxHeight
           Picture_Width = MaxWidth
       End If

       'Maalataan kuva uudessa koossa
       Dim Scaled_Image As New Bitmap(Picture_Width, Picture_Height)
       Dim Maalari As Graphics = Graphics.FromImage(Scaled_Image)
       Maalari.DrawImage(SourceImage, 0, 0, Picture_Width, Picture_Height)

       Maalari.Dispose()
       SourceImage.Dispose()

       Return Scaled_Image
   End Function

Posted

Thats because they are VB specific use syntax like:

dblvar=(double) expression;
intvar=(int) expression;

Afraits

"The avalanche has started, it is too late for the pebbles to vote"

Posted

(borrowed from Instant C# - http://www.instantcsharp.com)

 

private Bitmap ScaleImage(Bitmap SourceImage, int MaxWidth, int MaxHeight)

{

double OriginalAspectRatio = System.Convert.ToDouble(MaxWidth / MaxHeight);

double PicturesAspectRatio = System.Convert.ToDouble(SourceImage.Width / SourceImage.Height);

int Picture_Width = 0;

int Picture_Height = 0;

 

if (OriginalAspectRatio > PicturesAspectRatio)

{

//Skaalataan korkeuden mukaan

Picture_Height = MaxHeight;

Picture_Width = System.Convert.ToInt32(PicturesAspectRatio * Picture_Height);

}

else if (OriginalAspectRatio < PicturesAspectRatio)

{

//Skaalataan leveyden mukaan

Picture_Width = MaxWidth;

Picture_Height = System.Convert.ToInt32(Picture_Width / PicturesAspectRatio);

}

else if (OriginalAspectRatio == PicturesAspectRatio)

{

Picture_Height = MaxHeight;

Picture_Width = MaxWidth;

}

 

//Maalataan kuva uudessa koossa

Bitmap Scaled_Image = new Bitmap(Picture_Width, Picture_Height);

Graphics Maalari = Graphics.FromImage(Scaled_Image);

Maalari.DrawImage(SourceImage, 0, 0, Picture_Width, Picture_Height);

 

Maalari.Dispose();

SourceImage.Dispose();

 

return Scaled_Image;

}

Posted
Well for starters I can't get CDbl() and CInt() to work.

 

These are part of legacy VB and you shouldn't use them. One choice is using implicit or explicit conversion, this is from my notes...

 

// Implicit - when something may not convert such as string into a number

int myNumber = (int)myString;

 

// Explicit - when something will always convert such as number into a string

int myNumber = Convert.ToInt16(myString);

Experience is something you don't get until just after the moment you needed it
Posted
These are part of legacy VB and you shouldn't use them. One choice is using implicit or explicit conversion, this is from my notes...

 

// Implicit - when something may not convert such as string into a number

int myNumber = (int)myString;

 

// Explicit - when something will always convert such as number into a string

int myNumber = Convert.ToInt16(myString);

 

Actually, the "Convert" class functions correspond more closely to the VB functions. Also, I think you have it reversed - the compiler will only let you do a (int) type of cast when it is guaranteed to convert.

  • Leaders
Posted

An implicit conversion would be one like this, where the conversion is not explicitly stated (therefore, implied):

Dim X As Integer = "12"
Dim Y As Integer = 1.0F

What is the behavioral difference between (int)String and Convert.ToInt32(String)? Shouldn't they both throw an exception if the number format is invalid and succeed if it isn't?

 

(Also, note that functions like CInt are not there for legacy purpose. For conversion between numeric types, these "functions" are inlined, where as calls to the Convert class are not, and CInt is much easier to use than the clumsy CType casting syntax.)

[sIGPIC]e[/sIGPIC]
Posted
The compiler will not let you do (int) in some cases where you can do Convert.ToInt32. i.e., The Convert class methods do more data massaging, while casting with the () operator only is there to make the compiler happy that you're not mixing and matching types.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...