将下面VB代码转化为C#代码
Public Sub PictureToCenter(tPic As Picture, PicBox As PictureBox)Dim PicH As Long, PicW As Long
Dim PicBoxW As Long, PicBoxH As Long
Dim PicRate As Single, PicBoxRate As Single
Dim NewH As Long, NewW As Long
PicBoxH = PicBox.ScaleHeight
PicBoxW = PicBox.ScaleWidth
PicBoxRate = PicBoxW / PicBoxH
PicW = ScaleX(tPic.Width, vbHimetric, PicBox.ScaleMode)
PicH = ScaleY(tPic.Height, vbHimetric, PicBox.ScaleMode)
PicRate = PicW / PicH
PicBox.Cls
If PicH <= PicBoxH And PicW <= PicBoxW Then
PicBox.PaintPicture tPic, (PicBoxW - PicW) / 2, (PicBoxH - PicH) / 2
Exit Sub
End If
If PicBoxRate < PicRate Then
NewH = PicBoxW / PicRate
PicBox.PaintPicture tPic, 0, (PicBoxH - NewH) / 2, PicBoxW, NewH
Else
NewW = PicBoxH * PicRate
PicBox.PaintPicture tPic, (PicBoxW - NewW) / 2, 0, NewW, PicBoxH
End If
End Sub
--------------------编程问答-------------------- Public PictureToCenter(Picture tPic, PictureBox PicBox)
Long PicH, PicW
, PicBoxW, PicBoxH
, NewH,NewW Single PicRate, PicBoxRate
PicBoxH = PicBox.ScaleHeight
PicBoxW = PicBox.ScaleWidth
PicBoxRate = PicBoxW / PicBoxH
PicW = ScaleX(tPic.Width, vbHimetric, PicBox.ScaleMode)
PicH = ScaleY(tPic.Height, vbHimetric, PicBox.ScaleMode)
PicRate = PicW / PicH
PicBox.Cls
If (PicH <= PicBoxH && PicW <= PicBoxW){ PicBox.PaintPicture tPic, (PicBoxW - PicW) / 2, (PicBoxH - PicH) / 2
} If (PicBoxRate < PicRate)
{NewH = PicBoxW / PicRate
PicBox.PaintPicture tPic, 0, (PicBoxH - NewH) / 2, PicBoxW, NewH}
Else
{NewW = PicBoxH * PicRate
PicBox.PaintPicture tPic, (PicBoxW - NewW) / 2, 0,NewW, PicBoxH}
--------------------编程问答-------------------- Public PictureToCenter(Picture tPic, PictureBox PicBox)
{Long PicH, PicW
,PicBoxW,PicBoxH
,NewH,NewW; Single PicRate,PicBoxRate;
PicBoxH = PicBox.ScaleHeight
; PicBoxW = PicBox.ScaleWidth;
PicBoxRate = PicBoxW / PicBoxH
; PicW = ScaleX(tPic.Width, vbHimetric, PicBox.ScaleMode)
; PicH = ScaleY(tPic.Height, vbHimetric, PicBox.ScaleMode);
PicRate = PicW / PicH
; PicBox.Cls;
If (PicH <= PicBoxH && PicW <= PicBoxW){ PicBox.PaintPicture tPic, (PicBoxW - PicW) / 2, (PicBoxH - PicH) / 2;
} If (PicBoxRate < PicRate)
{NewH = PicBoxW / PicRate
PicBox.PaintPicture tPic, 0, (PicBoxH - NewH) / 2, PicBoxW, NewH;}
Else
{NewW = PicBoxH * PicRate
PicBox.PaintPicture tPic, (PicBoxW - NewW) / 2, 0,NewW, PicBoxH;}} --------------------编程问答-------------------- PicBox.PaintPicture tPic, (PicBoxW - NewW) / 2, 0,NewW, PicBoxH;}}看不懂。。。
--------------------编程问答-------------------- 貌似不用这么复杂吧,WinForms里的PictureBox有一个SizeMode属性,好象可以满足你的需要。 --------------------编程问答--------------------
看不懂就对了,因为那代码既不是VB也不是C#。
这代码的意思其实很简单,就是对图片缩放处理,你可以在很多系统上看到类似的实现。
算法的原理无非是首先判断图片的长宽比例和尺寸。
然后在一个给定的尺寸范围中保持图片的比例,得到符合这个尺寸比例最大的图像。 --------------------编程问答-------------------- 大哥能给我C#的代码吗?大概功能就是对图片缩放处理,然后居中显示在picturebox中,谢谢了,有急用。。
--------------------编程问答-------------------- Graphics g=this.picture.CreateGraphics();
g.drawimage();//根据图片的大小算坐标。
--------------------编程问答--------------------
既然方法都有了,写程序有什么困难呢?难道你从来没有写过程序,甚至没有想象下,你除了粘贴别人的程序还可以自己创造点什么? --------------------编程问答--------------------
只有自己实践才能掌握,虽软件的根本是算法,但基础的语名应用是必须的,不能拨苗助长,否则你永远学不到东西的。
补充:.NET技术 , C#