翻译:虚拟相册



    在用崭新的数码相机拍摄到一些漂亮图片,并把它们下载到电脑上后,你接下来可以做些什么呢?呵呵,你可以使用灵巧的《虚拟相册》来管理你的这些珍藏。你可以直接从资源管理器中拖动图片到相册中,你也可以点击图片下边的文本框来编辑图片标题。相册的数据存储在一个XML文件中。《虚拟相册》允许你打印相册中的当前页。下边是虚拟相册窗体类的UML描述。



    这篇文章覆盖了两方面《虚拟相册》使用到的.Net技术。

    1.怎么实现从资源管理器到窗体的托放

    2.怎样正确地使用XML的特性,对数据进行读、写、过滤和更新。

    在资源管理器上实现拖放比较容易。在拖动内容到窗体的时候,你只需关心在进行放操作前,资源管理器已经处理了拖操作。首先,你需要设置窗体的AllowDrop属性为True,这样窗体才能接收到拖动对象,然后我们通过DragEnter事件来改变鼠标在窗体上拖动时的样式为“copy drop”。


private void Form1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
  e.Effect 
= DragDropEffects.Copy;  // set the cursor to show a drop copy
}

    下边,需要使用DragDrop事件来处理鼠标拖动到窗体上的对象。这个事件会检测拖动的DataFormat是否为FileDrop,如果是,则取出文件名。然后使用这一文件名载入图片,并把它显示到相应的picturebox里。

private void Form1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
  
string theFile;
  
try
   
{
         
// check to make sure the dropped item is of type FileDrop
          if (e.Data.GetDataPresent(DataFormats.FileDrop))
          
{
             
object filename = e.Data.GetData(DataFormats.FileDrop);
             theFile 
= (string)((System.Array)filename).GetValue(0);
             LoadPictureBox(theFile, e.X, e.Y);   
//  Load the image using the path name  into the form
          }

    }

  
catch (Exception ex)
   
{
       MessageBox.Show(ex.Message.ToString());
   }
 
}


        使用XML

        XML就是存在于内存中的数据库,它的工作和数据集非常的类似。通过XML.Net对象间方便的联系,你几乎可以在DataSet里执行所有必要的数据操作。下边的代码读取XML中数据到虚拟相册的某一页里。首先,使用ReadXml函数把数据从XML文件读到数据对象里,然后从数据对象里读出,并把它们显示窗体中。

void ReadPage()
{
  ds.Clear();   
// clear out the dataset

// if the album xml database exists, read it in.
  if (File.Exists("albumdata.xml"))
  
{
   ds.ReadXml(
"albumdata.xml");
   ReadPageFromDataset();   
// read dataset into form
  }
 
 PageNumberLabel.Text 
= (CurrentPage + 1).ToString();
}


    ReadPageFromDataSet这个方法里,将会进行一次长度为四的循环,每次都按照选择标准来筛选每一数据行,并把他们显示到窗体里。

void ReadPageFromDataset()
 
{
   DataTable dt 
= ds.Tables["Album"];
   DataView dv 
= new DataView(dt);
   
for (int i = 0; i < 4; i++)
    
{
// set up a filter to filter out a single row using the Position of the image and the  Page Number
      dv.RowFilter = "Page = " + CurrentPage.ToString() + " AND Position = " + i.ToString();
      
if (dv.Count > 0// check to see if a row exists
       {
         DataRowView drv 
= dv[0];
         pictures[i].Image 
= Image.FromFile(drv["ImageName"].ToString());
         labels[i].Text 
= drv["Caption"].ToString();
       }
 
      
else
       
{
        
// clear out image
        pictures[i].Image = null;
        labels[i].Text 
= "";
       }

     }

  }
 


    插入一张图片到XML数据库里同样简单。下边的代码会检查数据集,看是否存在需要插入的图片。如果有,则会更新现有的图像数据文件,否则,就会使用数据集中包含的功能强大的类来插入新的图像数据到XML数据文件中。

void InsertTheImage(string theFile)
 
{

// try to find the image row from the Position and Page we are inserting the image into
   DataTable dt = ds.Tables["Album"];
   DataView dv 
= new DataView(dt);
   dv.RowFilter 
= "Page = " + CurrentPage.ToString() + " AND Position = " + CurrentPosition.ToString();

// check to see if the row exists
   if (dv.Count > 0)
   
{
//  image row exists, update it.
      DataRowView drv = dv[0];
      
object primarykey = drv["ID"]; 
// update the dataset using the primary key to find the particular row containing the image
       DataRow drFound = dt.Rows.Find(new object[]{primarykey});
       drFound[
"ImageName"= theFile;
       drFound[
"Caption"= labels[CurrentPosition].Text;
    }

  
else
   
{
     
// its a new page and position,  insert it into the dataset
    DataRow dr = dt.NewRow();
    dr[
"ID"= dt.Rows.Count;
    dr[
"ImageName"= theFile;
    dr[
"Position"= CurrentPosition;
    dr[
"Page"= CurrentPage;
    dr[
"Caption"= labels[CurrentPosition].Text;
    dt.Rows.Add(dr);
  }


  
// write out the new data to the XML database

  ds.WriteXml(
"albumdata.xml");

}


    结论

    使用.Net,进行数据操作非常方便,你可以通过XML与数据集的结合从XML数据文件里选取特定的信息,分类XML数据文件,甚至是对XML文件里的数据进行计算。当有大量的数据在内存中装配好的时候,如果你要对应用程序需使用数据进行简单的操作,使用XML或许是最有效率的方法。 

    下载代码


    (刚试着翻译文章,有什么不妥的地方,请各位指出,www.csnote.com)

原文地址:https://www.cnblogs.com/ipqn/p/118267.html