Click here to Skip to main content
12,895,774 members (52,100 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB .NET WPF VB.NET .NET4.5
Here goes:

I want to set a background image on a Canvas, using ImageBrush. The trouble starts when I want to resize the Canvas and leave the background picture intact. Meaning it will stay the same size in the topleft corner.

This is what I tried:
 Private Function GenerateImageBrush(ByVal image As ImageSource, Optional ByVal SizeNew As Boolean = False) As ImageBrush
        Dim result As New ImageBrush
 
        Dim drawingVisual As New DrawingVisual()
        Dim drawingContext As DrawingContext = drawingVisual.RenderOpen()
        drawingContext.DrawRectangle(Brushes.White, New Pen(Brushes.Black, 1), New Rect(New Size(Me.Height, Me.Width)))
        drawingContext.DrawImage(image, New Rect(0, 0, imgWidth, imgHeight))
        drawingContext.Close()
 
        Dim bmp As New RenderTargetBitmap(Me.Height, Me.Width, 96, 96, PixelFormats.Pbgra32)
        bmp.Render(drawingVisual)
 
        result.ImageSource = bmp
        Return result
    End Function
    
    Dim imgWidth, imgHeight as Double
    
    Public Sub AddPicture(ByVal pic As String)
        Dim imgConv = New ImageSourceConverter()
        Dim imageSource As ImageSource
        imageSource = DirectCast(imgConv.ConvertFromString(pic), ImageSource)
        
        imgWidth = imageSource.Width
        imgHeight = imageSource.Height
 
        Me.Width = imageSource.Width
        Me.Height = imageSource.Height
 
        Me.Background = GenerateImageBrush(imageSource)
    End Sub


The idea is to return an Image that has preserved its original size, and filled the rest of the space with white color.

But When I try to resize the canvas, the picture starts to deform. It seems to want to preserve the original ratio Length/Width and adusts the image accordingly. But I cant seem to find out how to do this, does anybody know how to get this to work?
Posted 22-Sep-12 23:15pm
Updated 22-Sep-12 23:16pm
v2
Comments
Sandeep Mewara 23-Sep-12 6:14am
   
5! for proper structured question!
Kenneth Haugland 23-Sep-12 17:07pm
   
Thanks. A hard problem, and I did not find a solution, but found a workaround that is ok.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

I changed the code to make it work. I generated an own picture class:
Public Class PictureElement
    Inherits FrameworkElement
 
    Private _children As VisualCollection
 
    Public Sub New(p_position As Point, ByVal Url As String)
        'You have to set the position before creating the Visual collection
        'Other wise the object will be placed on point (0,0)
 
        PositionOnCanvas = p_position
 
        _children = New VisualCollection(Me)
        _children.Add(CreateDrawingVisualImage(Url))
    End Sub
 
    Private Function CreateDrawingVisualImage(ByVal img As String) As DrawingVisual
        Dim imgConv = New ImageSourceConverter()
        Dim imageSource As ImageSource
        imageSource = DirectCast(imgConv.ConvertFromString(img), ImageSource)
        Dim imgHeight = imageSource.Height
        Dim imgWidth = imageSource.Width
 

        ' Create a circle and draw it in the DrawingContext.
        Dim drawingVisual As New DrawingVisual()
        Dim drawingContext As DrawingContext = drawingVisual.RenderOpen()
        drawingContext.DrawImage(imageSource, New Rect(0, 0, imgWidth, imgHeight))
        drawingContext.Close()
 
        ' Persist the drawing content.
        ' drawingContext.Close()
 
        Return drawingVisual
    End Function
 
......


Would love to know how to do it with my original method though....
  Permalink  

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

    Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 23 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100