我试图将一个图像文件从硬盘加载到GTK#.I中的图像小部件中,知道Pixbuf是用来表示我使用过的image.In .net的Bitmap b=Bitmap.from File ("c:\windows\file.jpg")
并分配PictureBox=b;
我怎样才能用Image Widget做到这一点?
更新:
我试过了
protected void OnButton2ButtonPressEvent (object o, ButtonPressEventArgs args)
{
var buffer = System.IO.File.ReadAllBytes ("i:\\Penguins.jpg");
var pixbuf = new Gdk.Pixbuf (buffer);
image103.Pixbuf = pixbuf;
}但不起作用。
发布于 2013-12-09 11:46:37
试试这个:
var buffer = System.IO.File.ReadAllBytes ("path\\to\\file");
var pixbuf = new Gdk.Pixbuf (buffer);
image.Pixbuf = pixbuf;此外,您还可以创建像这样的像素:
var pixbuf = new Gdk.Pixbuf ("path\\to\\file");但是,当我试图使用包含一些俄罗斯符号的路径的构造函数时,我有一个异常,因为编码错误。
更新我不知道在gtk#图像拉伸选项中设置的任何遗留方法,通常通过创建新控件来解决这个问题。因此,右键单击项目->Add->Create并将名称设置为ImageControl。在创建的小部件上添加图像。然后像这样编辑ImageControl's代码:
[System.ComponentModel.ToolboxItem (true)]
public partial class ImageControl : Gtk.Bin
{
private Pixbuf original;
private bool resized;
public Gdk.Pixbuf Pixbuf {
get
{
return image.Pixbuf;
}
set
{
original = value;
image.Pixbuf = value;
}
}
public ImageControl ()
{
this.Build ();
}
protected override void OnSizeAllocated (Gdk.Rectangle allocation)
{
if ((image.Pixbuf != null) && (!resized)) {
var srcWidth = original.Width;
var srcHeight = original.Height;
int resultWidth, resultHeight;
ScaleRatio (srcWidth, srcHeight, allocation.Width, allocation.Height, out resultWidth, out resultHeight);
image.Pixbuf = original.ScaleSimple (resultWidth, resultHeight, InterpType.Bilinear);
resized = true;
} else {
resized = false;
base.OnSizeAllocated (allocation);
}
}
private static void ScaleRatio(int srcWidth, int srcHeight, int destWidth, int destHeight, out int resultWidth, out int resultHeight)
{
var widthRatio = (float)destWidth / srcWidth;
var heigthRatio = (float)destHeight / srcHeight;
var ratio = Math.Min(widthRatio, heigthRatio);
resultHeight = (int)(srcHeight * ratio);
resultWidth = (int)(srcWidth * ratio);
}
}现在,您可以使用ImageControl's小部件的Pixbuf属性设置图片。
https://stackoverflow.com/questions/20469706
复制相似问题