split 1 image into several different ones

  • split 1 image into several different ones

    Last post 2011/7/26 3:18 by  Orencosoft. 3 replies.
    •  2011/7/24 18:55 
      Split 1 image into several different ones
      Hello,

      I have one image that is loaded from the web that acutally contains several seperate images. For example a 200x200 image that contains 4 100x100 images.

      I would like to load that picture from the internet once and then show the 100x100 images in several image-objects in the xaml.

      How could I do that?
        
    •  2011/7/25 17:54In reply to
      Re: Split 1 image into several different ones
      No ideas?

      I mean is there any way I could set which part of the image is actually shown?

      I thought about creating a image object with a size of 200x200 pixels to contain the entire image and then wrap this inside of a 100x100 scroll viewer and set the position programatically. Is this possible?
        
    •  2011/7/25 19:53In reply to
      Re: Split 1 image into several different ones
      OK I have made a little progress.

      I have wrapped the image into a scrollviewer and then I use  ScrollToVerticalOffset to scroll to the correct position. However, I would like the image to be "stuck" in this position and hence I would like to set the  VerticalScrollBarVisibility to Disabled but if I do that (even after scrolling), the image does not scroll to the correct position.

      What am I doing wrong?
        
    •  2011/7/26 3:18In reply to
      Re: Split 1 image into several different ones

      Not sure scrolling around would be the best solution. I would use a writable bitmap and copy the sections of the picture you wanted.

       

      A quick example, been awhile since I have use a writable bitmap. Tested the code, seems to work ok. Code is written to load an image from iso storage, display the picture in image1, copy 100x100 pixels and display in image2.

       

      Change the offsets and width/height to copy whatever you want, something to experiment with.



          public partial class MainPage : PhoneApplicationPage  
          {  
              // Constructor  
              public MainPage()  
              {  
                  InitializeComponent();  
                  GetImageFromIso();  
              }  
       
       
              BitmapImage bMap;  
              void GetImageFromIso()  
              {  
                  bMap = new BitmapImage(new Uri("testPic.jpg", UriKind.RelativeOrAbsolute));  
                  bMap.ImageOpened += new EventHandler<RoutedEventArgs>(BMapImageOpened); // make sure it is fully loaded  
                  image1.Source = bMap;   // display full image  
              }  
       
              void BMapImageOpened(object sender, RoutedEventArgs e)  
              {  
                  WriteableBitmap sourcePicture = new WriteableBitmap(bMap);    // make a writable bitmap  
                  image2.Source = CopyArea(sourcePicture, 0, 0, 100, 100);    // copy 100x100 pixels from the upper left corner and display  
              }  
       
              WriteableBitmap CopyArea(WriteableBitmap inputWB, int xOffset, int yOffset, int width, int height)  
              {  
                  int SourceWidth = inputWB.PixelWidth;  
                  WriteableBitmap outputWB = new WriteableBitmap(width, height);  
                  for (int y = 0; y <= height - 1; y++)  
                  {  
                      int SourceIndex = xOffset + (yOffset + y) * SourceWidth;  
                      int DestIndex = y * width;  
                      Array.Copy(inputWB.Pixels, SourceIndex, outputWB.Pixels, DestIndex, width);  
                  }  
                  return outputWB;  
              }  
          }  
       
       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值