Programmatically Rearranging Images

Questions, comments and suggestions concerning VintaSoft Imaging .NET SDK.

Moderator: Alex

Post Reply
tplambeck
Posts: 17
Joined: Wed May 08, 2019 7:09 pm

Programmatically Rearranging Images

Post by tplambeck »

I've got an image viewer displaying a multipage PDF. I need to move the pages up or down in the document using a context menu. Moving them up in the image collection works fine, down not so much. I'm sure I have a simple mistake in my code here. Does anyone see the issue?

Code: Select all

private void moveUpToolStripMenuItem_Click(object sender, EventArgs e)
{
    int curIndex = pdfViewer.FocusedIndex;
    int[] vwIndex = new int[1] {pdfViewer.FocusedIndex};
    
    if (curIndex > 0)
    {
        pdfViewer.Images.MoveRange(curIndex - 1, vwIndex);
        pdfViewer.FocusedIndex = curIndex - 1;
    }
}

private void moveDownToolStripMenuItem_Click(object sender, EventArgs e)
{
    int curIndex = pdfViewer.FocusedIndex;
    int[] vwIndex = new int[1] {pdfViewer.FocusedIndex};

    if (curIndex < pdfViewer.Images.Count - 1)
    {
        pdfViewer.Images.MoveRange(curIndex + 1, vwIndex);
        pdfViewer.FocusedIndex = curIndex + 1;
    }
}
Alex
Site Admin
Posts: 2308
Joined: Thu Jul 10, 2008 2:21 pm

Re: Programmatically Rearranging Images

Post by Alex »

Hello,

You have the problem with the moveDownToolStripMenuItem_Click method because your code has logical mistake. In your code you are trying to insert element with index "curIndex" into position "curIndex + 1" and this means that you are trying to insert element after itself - this action will not change order of images in image collection.

Here is correct code for moveDownToolStripMenuItem_Click method:

Code: Select all

private void moveDownToolStripMenuItem_Click(object sender, EventArgs e)
{
    int curIndex = thumbnailViewer1.FocusedIndex;
    int[] vwIndex = new int[1] { thumbnailViewer1.FocusedIndex };

    if (curIndex < thumbnailViewer1.Images.Count - 1)
    {
        thumbnailViewer1.Images.MoveRange(curIndex + 2, vwIndex);
        thumbnailViewer1.FocusedIndex = curIndex + 1;
    }
}
Best regards, Alexander
tplambeck
Posts: 17
Joined: Wed May 08, 2019 7:09 pm

Re: Programmatically Rearranging Images

Post by tplambeck »

Thanks, Alex makes perfect sense now. Once again, great support!
Post Reply