2012-11-08 36 views
2

我试图让PictureBox的数组显示图片列表(以PNG文件格式)。什么是存储项目相关图片的好方法?

我试图使用.NET ImageList控件,但它坚持重新调整我的图片大小。它也不支持那些png文件的透明背景。

我也试过用Assembly检索像这样我的文件:
_imageStream = _assembly.GetManifestResourceStream("MyNamespace.MyImage.png"); 但代码不会返回我任何资源文件,也没有抛出任何运行时错误。

我的问题是,有没有其他方法可以做到这一点?或者更好的是,我能以某种方式使ImageList控件不会改变我的照片吗?谢谢。

回答

0

你可以尝试这样的事情,虽然我不知道这是最好的一个或不会: -

Assembly ambly = Assembly.LoadFile(pathToDll); 

BitMap bitMap; 
// where "ns" is the default namespace of the resource project  
using (Stream resourceStream = ambly.GetManifestResourceSream("ns.image.jpg")) 
{ 
    bitMap = BitMap.FromStream(resourceStream); 
} 

一个例子: -

interface IThemeResourceProvider 
{ 
Stream LoadBigLogo(); 
Stream LoadSmallLogo(); 
} 

然后在资源库中实现该接口

public class ThemeResourceProvider : IThemeResourceProvider 
{ 
public Stream LoadBigLogo() 
{ 
    Assembly ambly = Assembly.GetExecutingAssembly(); 
    return ambly.GetManifestResourceStream("namespace.image.jpg"); 
    } 

    (...) 
    } 

最后,而不是在主应用程序直接加载资源,实例化IThemeResourceProvider在资源库中

Assembly assembly = Assembly.LoadFile(pathToDll); 

    var results = from type in assembly.GetTypes() 
      where typeof(IThemeResourceProvider).IsAssignableFrom(type) 
      select type; 

发现现在你已经在该列表中一个IEnumerable。通常情况下,你只有一个,但使用这种方法,你也可以托管多组资源,并在同一资源DLL中实现多个IThemeResourceProviders。你可以例如用名称标识每个IThemeResourceProvider,或者作为属性,或者在各种实现上使用自定义[Attribute]装饰。我会把剩下的东西留给你弄清楚。

但在这里是如何实例化IThemeResourceProviders在列表

foreach (var providerType in results) 
    { 
    var constructorInfo = providerType.GetConstructor(Type.EmptyTypes); 
    IThemeResourceProvider provider = constructorInfo.Invoke(null); 
    } 

,最后使用这些供应商之一获得一个位图:

BitMap bitMap; 
    using (Stream resourceStream = provider.LoadBigLogo()) 
    { 
    bitMap = BitMap.FromStream(resourceStream); 
    } 
0

这是我从别人得到的代码它对我来说很好用!

private void SetImage(PictureBox pb) { 
     try { 
      Image img = pb.Image; 

      Size imgSize = GenerateImageDimensions(img.Width, img.Height, pb.Width, pb.Height); 
      Bitmap finalImg = new Bitmap(img, imgSize.Width, imgSize.Height); 
      Graphics gfx = Graphics.FromImage(img); 
      gfx.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 

      pb.Image = null; 
      pb.SizeMode = PictureBoxSizeMode.AutoSize; 
      pb.Image = finalImg; 
     } catch(Exception ex) { 

     } 
    } 
    public Size GenerateImageDimensions(int currW, int currH, int destW, int destH) { 
     //double to hold the final multiplier to use when scaling the image 
     double multiplier = 0; 

     //string for holding layout 
     string layout; 

     //determine if it's Portrait or Landscape 
     if(currH > currW) layout = "portrait"; 
     else layout = "landscape"; 

     switch(layout.ToLower()) { 
      case "portrait": 
       //calculate multiplier on heights 
       if(destH > destW) { 
        multiplier = (double) destW/(double) currW; 
       } else { 
        multiplier = (double) destH/(double) currH; 
       } 
       break; 
      case "landscape": 
       //calculate multiplier on widths 
       if(destH > destW) { 
        multiplier = (double) destW/(double) currW; 
       } else { 
        multiplier = (double) destH/(double) currH; 
       } 
       break; 
     } 

     //return the new image dimensions 
     return new Size((int) (currW * multiplier), (int) (currH * multiplier)); 
    } 

编辑:充分披露我的所有图像都是jpg,所以我不知道这将如何处理透明背景。编辑二:您还需要调整pb.SizeMode以适应您的需求。我这样做的方式是为PictureBox设置最大尺寸,并且运行良好。

相关问题