JumpyNET Posted June 1, 2005 Posted June 1, 2005 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 Quote
stustarz Posted June 1, 2005 Posted June 1, 2005 Which actual lines are you having a problem with? Quote Visit: VBSourceSeek - The VB.NET sourcecode library "A mere friend will agree with you, but a real friend will argue."
JumpyNET Posted June 1, 2005 Author Posted June 1, 2005 (edited) Well for starters I can't get CDbl() and CInt() to work. Edited June 1, 2005 by JumpyNET Quote
Afraits Posted June 1, 2005 Posted June 1, 2005 Thats because they are VB specific use syntax like: dblvar=(double) expression; intvar=(int) expression; Quote Afraits "The avalanche has started, it is too late for the pebbles to vote"
Jaco Posted June 2, 2005 Posted June 2, 2005 (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; } Quote
travisowens Posted June 3, 2005 Posted June 3, 2005 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); Quote Experience is something you don't get until just after the moment you needed it
Jaco Posted June 3, 2005 Posted June 3, 2005 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. Quote
Leaders snarfblam Posted June 3, 2005 Leaders Posted June 3, 2005 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.) Quote [sIGPIC]e[/sIGPIC]
Jaco Posted June 3, 2005 Posted June 3, 2005 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.