try


{


    // Create an Image object from a file.


    // PhotoTextBox.Text is the full path of your image


    using (Image photoImg = Image.FromFile(PhotoTextBox.Text))


    {


        // Create a Thumbnail from image with size 50×40.


        // Change 50 and 40 with whatever size you want


        using (Image thumbPhoto = photoImg.GetThumbnailImage(50, 40, null, new System.IntPtr()))


        {


            // The below code converts an Image object to a byte array


            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())


            {


                thumbPhoto.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);


                imgBytes = ms.ToArray();


            }


        }


    }


}


catch (Exception exp)


{


    MessageBox.Show(“Select a valid photo!”);


    PhotoTextBox.Select();


    return;


}        


C#中让ListBox支持文件路径的拖放
有时,我们程序,需要加载一个文件列表,这个列表,通常用ListBox来存放,为了方便操作,让listBox支持拖放入文件的路径,是个非常好的功能,在.NET里面实现这个功能,是非常方便,只需要3个步骤:


将 ListBox 的 AllowDrop 属性设为 true
在 ListBox 的 DragOver 和 DragEnter 事件中处理
void ListBox1DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}


void ListBox1DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
上面的 e.Effect 可以根据实际情况,调节不同的值,不过,如果不确定,就用默认的All


3. 处理拖放事件


void ListBox1DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
{
String[] files = (String[])e.Data.GetData(DataFormats.FileDrop);
foreach (String s in files)
{
(sender as ListBox).Items.Add(s);
}
}

历史博文

标签:,
十月 23, 2009 at 11:37 下午 by yippee 10 次
Category: Dev
Tags: ,