Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a large Canvas which can be scrolled horizontally. My Canvas contains little lines (StrokeThickness = 1) positioned uniformly. When I scroll to right the lines start to disappear and appear when reaching an horizontal offset of 8.000.000 (more or less). How can I fix it?

Here an example. 10 lines to left, 10 lines to right. The first lines are visible while the last ones are partially visible (5 visible, 5 not).
XML
<Window x:Class="WpfApplication.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <ScrollViewer HorizontalScrollBarVisibility="Visible">
            <Canvas Width="15000000">
                <Line Canvas.Left="0" X1="0" X2="0" Y1="0" Y2="100" Stroke="Black" StrokeThickness="1" />
                <Line Canvas.Left="5" X1="0" X2="0" Y1="0" Y2="100" Stroke="Black" StrokeThickness="1" />
                <Line Canvas.Left="10" X1="0" X2="0" Y1="0" Y2="100" Stroke="Black" StrokeThickness="1" />
                <Line Canvas.Left="15" X1="0" X2="0" Y1="0" Y2="100" Stroke="Black" StrokeThickness="1" />
                <Line Canvas.Left="20" X1="0" X2="0" Y1="0" Y2="100" Stroke="Black" StrokeThickness="1" />
                <Line Canvas.Left="25" X1="0" X2="0" Y1="0" Y2="100" Stroke="Black" StrokeThickness="1" />
                <Line Canvas.Left="30" X1="0" X2="0" Y1="0" Y2="100" Stroke="Black" StrokeThickness="1" />
                <Line Canvas.Left="35" X1="0" X2="0" Y1="0" Y2="100" Stroke="Black" StrokeThickness="1" />
                <Line Canvas.Left="40" X1="0" X2="0" Y1="0" Y2="100" Stroke="Black" StrokeThickness="1" />
                <Line Canvas.Left="45" X1="0" X2="0" Y1="0" Y2="100" Stroke="Black" StrokeThickness="1" />
                <Line Canvas.Left="14999950" X1="0" X2="0" Y1="0" Y2="100" Stroke="Black" StrokeThickness="1" />
                <Line Canvas.Left="14999955" X1="0" X2="0" Y1="0" Y2="100" Stroke="Black" StrokeThickness="1" />
                <Line Canvas.Left="14999960" X1="0" X2="0" Y1="0" Y2="100" Stroke="Black" StrokeThickness="1" />
                <Line Canvas.Left="14999965" X1="0" X2="0" Y1="0" Y2="100" Stroke="Black" StrokeThickness="1" />
                <Line Canvas.Left="14999970" X1="0" X2="0" Y1="0" Y2="100" Stroke="Black" StrokeThickness="1" />
                <Line Canvas.Left="14999975" X1="0" X2="0" Y1="0" Y2="100" Stroke="Black" StrokeThickness="1" />
                <Line Canvas.Left="14999980" X1="0" X2="0" Y1="0" Y2="100" Stroke="Black" StrokeThickness="1" />
                <Line Canvas.Left="14999985" X1="0" X2="0" Y1="0" Y2="100" Stroke="Black" StrokeThickness="1" />
                <Line Canvas.Left="14999990" X1="0" X2="0" Y1="0" Y2="100" Stroke="Black" StrokeThickness="1" />
                <Line Canvas.Left="14999995" X1="0" X2="0" Y1="0" Y2="100" Stroke="Black" StrokeThickness="1" />
            </Canvas>
        </ScrollViewer>
    </Grid>
</Window>


Thanks,

Raúl
Posted
Updated 6-May-11 4:22am
v3
Comments
Sergey Alexandrovich Kryukov 6-May-11 8:48am    
How can the lines disappear when they are "far away from left"? They should not appear in first place... Are you sure it's a problem? Explain more clearly, make a code sample. (Not post all your code, create a special simplest sample to show the problem!)
--SA
Raúl Nuño 6-May-11 10:20am    
Ok, I've edited the question and I've added a code sample.
TheyCallMeMrJames 6-May-11 10:30am    
Set your stroke thickness to 2 and try again. I think you're running into an anti-aliasing thing here.
Raúl Nuño 6-May-11 10:44am    
Yes, I know that making the stroke thickness bigger the lines don't disappear but I don't want to change it.

Instead of using a canvas that's THAT frickin' wide, I'd be owner drawing only what's visible. You're just asking for trouble trying to do it this way. Since a monitor normally isn't any wider than about 2000 pixels, there's no point in making an image that's 15,000,000 pixels wide.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-May-11 11:32am    
Good idea, my 5. (Original idea is just bad.) Zooming can be helpful, zoom to selection and other civilized stuff.
--SA
Raúl Nuño 9-May-11 4:23am    
I'm drawing only what's visible (elements in the visible part) and I need this big width in order to modify it (zoom in/out) and scroll the content (furthermore, I need to synchronize another panel scroll). What's the problem on having a wide canvas?
Dave Kreskowiak 9-May-11 10:49am    
The first is a bitmap, at 15,000,000 and ONE PIXEL HIGH, uses 45 MB of memory (32 bits per pixel). Does that sound like a good idea?

Your code is saying that your NOT drawing just what's visible. You're drawing the entire grid all at once. Your drawing surface should only be the size of the visible viewport.
Raúl Nuño 3-Jun-11 18:25pm    
Sorry for the delay, I have been a little busy...

Are you sure WPF canvas maintains the state of each pixel in memory? I can make an Int32.MaxValue x Int32.MaxValue canvas without getting System.OutOfMemoryException. I'm curious on that.

And YES, I'm drawing just what's visible. The code above is a simple example to show my problem.

In summary, I can't have a wide canvas?
In this page http://connect.microsoft.com/VisualStudio/feedback/details/731467/disappearing-path-wp[^] you can find the reason of why the lines disappear.
I had the same problem, then i split the lines in function of its length
 
Share this answer
 
Comments
Raúl Nuño 28-Oct-12 10:37am    
Wow... long time since I posted the question. I can't remember how I solved this.

So this is a WPF limitation. Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900