GithubHelp home page GithubHelp logo

Comments (2)

cyotek avatar cyotek commented on July 19, 2024

Good grief - this never crossed my mind. Curiously enough I had a support ticket recently for another user who was having difficulties with the ImageBox and giant images. I'd made a "rainy day note" after that to try and do a demo where multiple separate images are drawn as though they were a single image, similar to how things like online maps work. It just didn't twig that I could have done the same thing as you suggest.

Thanks very much for the tip - I will give that a go!

from cyotek.windows.forms.imagebox.

carlshe avatar carlshe commented on July 19, 2024

I write a control derived from ImageBox. I would like to share the code here, and hope it could be useful. And hope the new version could be improved. Thanks!

public class PictureBox : ImageBox
{
public const float MaxDrawImageSidePixels = 5000;

    protected override void DrawImage(Graphics g)
    {
        Rectangle dstView = this.GetImageViewPort();
        RectangleF srcPortion = this.GetSourceImageRegion();
        if (srcPortion.Width <= MaxDrawImageSidePixels && srcPortion.Height <= MaxDrawImageSidePixels)
        {
            base.DrawImage(g);
            //TextRenderer.DrawText(g, srcPortion.ToString(), this.Font, this.ClientRectangle, this.ForeColor, this.BackColor, TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter | TextFormatFlags.WordBreak | TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix);
            return;
        }

        InterpolationMode currentInterpolationMode;
        PixelOffsetMode currentPixelOffsetMode;

        currentInterpolationMode = g.InterpolationMode;
        currentPixelOffsetMode = g.PixelOffsetMode;

        g.InterpolationMode = this.GetInterpolationMode();

        // disable pixel offsets. Thanks to Rotem for the info.
        // http://stackoverflow.com/questions/14070311/why-is-graphics-drawimage-cropping-part-of-my-image/14070372#14070372
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;

        RectangleF rfDst=new RectangleF(0,0,100,100);
        RectangleF rfSrc = new RectangleF(0, 0, 100, 100);
        try
        {
            // Animation. Thanks to teamalpha5441 for the contribution
            if (this.IsAnimating && !this.DesignMode)
            {
                ImageAnimator.UpdateFrames(this.Image);
            }
            int CountOfStepX = (int)Math.Round(srcPortion.Width / MaxDrawImageSidePixels + 0.5);
            int CountOfStepY = (int)Math.Round(srcPortion.Height / MaxDrawImageSidePixels + 0.5);

            float stepDstX = (int)(dstView.Width / CountOfStepX);
            float stepDstY = (int)(dstView.Height / CountOfStepY);
            float stepSrcX = (int)(srcPortion.Width / CountOfStepX);
            float stepSrcY = (int)(srcPortion.Height / CountOfStepY);
            for(int w=0;w< CountOfStepX;w++)
                for(int h=0; h<CountOfStepY;h++)
                {
                    rfDst = new RectangleF(dstView.X+ stepDstX*w, dstView.Y + stepDstY * h, stepDstX, stepDstY);
                    rfSrc=  new RectangleF(srcPortion.X+ stepSrcX *w, srcPortion.Y + stepSrcY * h, stepSrcX, stepSrcY);
                    g.DrawImage(this.Image, rfDst, rfSrc, GraphicsUnit.Pixel);

                    //Rectangle txtRect = new Rectangle((int)rfDst.X, (int)rfDst.Y, (int)rfDst.Width, (int)rfDst.Height);
                    //TextRenderer.DrawText(g, rfDst.ToString() + " " + rfSrc.ToString(), this.Font, txtRect, this.ForeColor, this.BackColor, TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter | TextFormatFlags.WordBreak | TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix);
                }
        }
        catch (Exception ex)
        {
            TextRenderer.DrawText(g, ex.Message, this.Font, this.ClientRectangle, this.ForeColor, this.BackColor, TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter | TextFormatFlags.WordBreak | TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix);
        }

        g.PixelOffsetMode = currentPixelOffsetMode;
        g.InterpolationMode = currentInterpolationMode;
    }
}

from cyotek.windows.forms.imagebox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.