locked
Zoom and moving picture (Size of a valid rec) RRS feed

  • Question

  • Hi,

     on my windows mobil application I want to show a zoomed moveble picture. My code:

        void Render()
        {
          IntPtr hdc;
    
          if (graphics == null)
            SpoilerView_Resize(null, null);
    
          try
          {
            if (!SpoilerImage.Equals(String.Empty))
            {
              FileStream st = new FileStream(SpoilerImage, FileMode.Open);
              IStream stream = new StreamOnFile(st);
              ImagingFactory factory = new ImagingFactoryClass();
              factory.CreateImageFromStream(stream, out FullImage);
              FullImage.GetImageInfo(out imageInfo);
    
              scale = (double)Width / (double)imageInfo.Width;
    
              if ((imageInfo.Height * scale) > Height)
                scale = (double)Height / (double)imageInfo.Height;
    
              uint ThumbWidth = (uint)((double)imageInfo.Width * scale);
              uint ThumbHeight = (uint)((double)imageInfo.Height * scale);
              FullImage.GetThumbnail(ThumbWidth, ThumbHeight, out ThumbImage);
              st.Close();
            }
          }
          catch (OutOfMemoryException)
          {
            MessageBox.Show("Not sufficient memory to load spoiler image!", "Sorry!");
            bitmap = null;
          }
     
          if (ThumbImage == null)
            return;
    
          if (!isZoomed)
          {
            graphics.FillRectangle(backBrush, 0, 0, Width, Height);
    
            Rectangle dstRect = new Rectangle((int)((Width - imageInfo.Width * scale) / 2), (int)((Height - imageInfo.Height * scale) / 2), (int)(imageInfo.Width * scale), (int)(imageInfo.Height * scale));
            hdc = graphics.GetHdc();
            ThumbImage.Draw(hdc, new RECT(dstRect), null);
            graphics.ReleaseHdc(hdc);
            graphics.Dispose();
          }
          else
          {
    
            graphics.FillRectangle(backBrush, 0, 0, Width, Height);
    
            if (offsetX > 1129) //-(imageInfo.Width - Width))
              offsetX = 1129; //-((int)imageInfo.Width - Width);
    
            if (offsetX < 0)
              offsetX = 0;
    
            if (offsetY > 1472) //-(imageInfo.Height - Height))
              offsetY = 1472; // -((int)imageInfo.Height - Height);
    
            if (offsetY < 0)
              offsetY = 0;
    
            Rectangle dstRect = new Rectangle((int)(0), (int)(0), Width, Height);
            Rectangle souRect = new Rectangle((int)(offsetX), (int)(offsetY), Width, Height);
            hdc = graphics.GetHdc();
            ImageInfo ThumbImageInfo;
            ThumbImage.GetImageInfo(out ThumbImageInfo);
    
            try
            {
              ThumbImage.Draw(hdc, new RECT(dstRect), new RECT(souRect));
            }
            catch { }
    
            graphics.ReleaseHdc(hdc);
            graphics.Dispose();
          }
    
          this.CreateGraphics().DrawImage(offScreenBmp, 0, 0);
        }
    
    
        private void SpoilerView_MouseMove(object sender, MouseEventArgs e)
        {
          if (!dragging)
            return;
    
          int diffX = dragStartX - e.X;
          int diffY = dragStartY - e.Y;
    
          dragStartX = e.X;
          dragStartY = e.Y;
    
          offsetX += diffX;
          offsetY -= diffY;
    
          Render();
        }
    
        private void SpoilerView_DoubleClick(object sender, EventArgs e)
        {
          bool oldZoom = isZoomed;
          isZoomed = !isZoomed && (imageInfo.Width > Width || imageInfo.Height > Height);
    
          if (oldZoom != isZoomed)
          {
            if (isZoomed)
            {
              // Seitenanteile [0..1] berechnen
              double curImgWidth = imageInfo.Width * scale;
              double curImgHeight = imageInfo.Height * scale;
    
              double borderX = (Width - curImgWidth) / 2;
              double borderY = (Height - curImgHeight) / 2;
    
              double clickOffsetX = lastClickX - borderX;
              double clickOffsetY = lastClickY - borderY;
    
              double ratioX = clickOffsetX / ((double)imageInfo.Width * scale);
              double ratioY = clickOffsetY / ((double)imageInfo.Height * scale);
    
              // Zu zentrierende Koordinate im Bildkoordinatensystem
              // berrechnen
              double centerX = (int)(ratioX * imageInfo.Width);
              double centerY = (int)(ratioY * imageInfo.Height);
    
              //gnargnar hier weitermachen
    
    //          offsetX = (int)(Width / 2 - centerX);
    //          offsetY = (int)(Height / 2 - centerY);
            }
    
            Render();
          }
        }
    
    

    I found the constant value for "offsetXY" by trail and error for one picture. But this values depends on the picture I have selected.

    But how can I get the right values? 

    If I uncommend the line

    //-(imageInfo.Width - Width))

    //-(imageInfo.Height - Height))

    I get the error "The parameter is incorrect." (I think form the codeline "new RECT(dstRect)" at the end of the Render () code listing)

    Here some values of my picture, I have tested:
    imageInfo.Width = 1944
    imageInfo.Height = 2591
    Width = 480
    Height = 672

    Thanks a lot and please excuse my bad english.
    Andreas  

    Wednesday, September 29, 2010 6:28 AM

All replies

  • Hi,

    I doesn't catch your question, could you explain it detailedly?

     

    Best regards,
    Guang-Ming Bian - MSFT


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Friday, October 1, 2010 6:03 AM
  • The source-code void Render() above, shows a picture in 2 way's

    1.) Show the picture a tumbnail (whole picture (2591x1944pixel) on the display(672x480pixel)) 

    source-code void Render() to end of if (!isZoomed). This is no problem and works well.

    2.) Show a zoomed cut-out of the picture (one pixel of the picture as one pixel on the display). It works also. Now the problem is, if I move the cut-out via mouse I get an exception "The parameter is incorrect".

    I think it comes from the line Rectangle souRect = new Rectangle((int)(offsetX), (int)(offsetY), Width, Height);

    because I don't know to calculate the maxvalue of offsetXY. At the first time I think max offsetXY is imageInfo.Height - Height but if I make it so, I get the exception if I move the cut-out to the upper-right of the image.

    So my question: Is anybody out here who can explain me who I have to calculate the max value of offsetXY

    Thanks a lot and please excuse may bad english

    Andreas

     

    Tuesday, October 5, 2010 1:05 PM