Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi guys, I presume this is a no-brainer for WPF coders?
Below is a function that returns an image source, but what I need to do is create a new jpg file, simple question is how do I convert a bitmap image to a file on disk? below is a basic func that I use to view thumbnails so I'm half way there.
any ideas would be helpful thanks.
VB
Public Function ScaleImageToFit(ByVal sourceImg As String, ByVal pbWidth As Integer, ByVal pbHeight As Integer) As BitmapImage

        Dim sourceImage As New BitmapImage()
        sourceImage.BeginInit()
        sourceImage.UriSource = New Uri(sourceImg, UriKind.Relative)
        sourceImage.EndInit()

        Dim scale_factor As Single = 0

        ' determine scale_factor based on source image size.
        Dim srcWidth As Integer = sourceImage.Width
        Dim srcHeight As Integer = sourceImage.Height
        Dim myImage As New Image

        If srcWidth > srcHeight Then  ' landscape
            scale_factor = pbWidth / srcWidth
            myImage.Width = pbWidth * scale_factor
            sourceImage.DecodePixelWidth = myImage.Width
        Else    ' portrait

            scale_factor = pbHeight / srcHeight  ' make it fit to pb height
            myImage.Height = pbWidth * scale_factor
            sourceImage.DecodePixelHeight = myImage.Height
        End If

        myImage.Source = sourceImage

        Return myImage.Source

    End Function
Posted
Updated 30-Jul-12 18:46pm
v4

1 solution

I don't know if this is a no-brainer for "WPF coders" (probably I never met one), but it is a no-brainer for a Google user:
http://stackoverflow.com/questions/4161359/save-bitmapimage-to-file[^].

Good luck,
—SA
 
Share this answer
 

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