?
Solved

Bitmap Image Too Large

Posted on 2010-08-20
4
Medium Priority
?
705 Views
Last Modified: 2012-06-27
I have code that add a company logo as a water mark to photos and then saves them back as a jpg.  Original images are 35kb, but the new image is 600kb.  The watermark is 3kb.  1) why is the new file so large?  2) How can I save these smaller?  
' Copy the foreground image into a bitmap.
        Dim phototouse As String
        phototouse = "C:\inetpub\wwwroot\watermark.png"        

        Dim picForeground As System.Drawing.Bitmap = New System.Drawing.Bitmap(phototouse)
        Dim fg_wid As Integer = picForeground.Width
        Dim fg_hgt = picForeground.Height
        Dim fg_bm As New Bitmap(fg_wid, fg_hgt)
        Using gr As Graphics = Graphics.FromImage(fg_bm)
            gr.DrawImage(picForeground, 0, 0, fg_wid, fg_hgt)
        End Using

        ' Make the red pixels transparent.
        fg_bm.MakeTransparent(Color.White)

        Dim photo As String = photoname
        ' Make the result bitmap.
        Dim picBackground As System.Drawing.Bitmap = New System.Drawing.Bitmap(localfolder & photo)
        Dim bg_wid As Integer = picBackground.Width
        Dim bg_hgt = picBackground.Height
        Dim bg_bm As New Bitmap(bg_wid, bg_hgt)
        If cy = 0 And cx = 0 Then
            'Need to position photo in far left corner b/c pattern not found
            cx = 0 ' Set to zero for logo in left bottom corner, set to bg_wid - 90 for logo in bottom right corner
            cy = bg_hgt - 20
        Else
            'pattern found, adjust to cover the pattern
            cx = cx - 2
            cy = cy - 11
        End If
        Using gr As Graphics = Graphics.FromImage(bg_bm)
            gr.DrawImage(picBackground, 0, 0, bg_wid, bg_hgt)

            ' Draw the foreground image on top.
            gr.DrawImage(fg_bm, cx, cy, fg_wid, fg_hgt)
        End Using

        ' Display the result.
        Dim destfile As String = localfolder & photonewname
        bg_bm.Save(destfile)

        ' We don't need the foreground bitmap any more.
        fg_bm.Dispose()

Open in new window

0
Comment
Question by:azyet24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 

Author Comment

by:azyet24
ID: 33490083
Attached are three images: original image, modified image and watermark

Comment by WallyMod.  Author requested the photos be removed as they contain the corporate identity.  Since the photos are not necessary for understanding of the question they have accordingly been removed.

WallyMod
Community Supoport Moderator
0
 
LVL 3

Accepted Solution

by:
rojasc earned 2000 total points
ID: 33490095
I think you are saving a bmp file in line 40 without jpeg compression


please read this page:
http://msdn.microsoft.com/en-us/library/bb882583.aspx
0
 

Author Comment

by:azyet24
ID: 33490125
rojasc, that worked great.  50% the photo is same size as original and quality looks good. thanks.
0
 

Author Comment

by:azyet24
ID: 33490130
Attached is the code from http://msdn.microsoft.com/en-us/library/bb882583.aspx that rojasc suggested
Private Sub VaryQualityLevel()
        ' Get a bitmap.
        Dim bmp1 As New Bitmap("c:\TestPhoto.jpg")
        Dim jgpEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Jpeg)

        ' Create an Encoder object based on the GUID
        ' for the Quality parameter category.
        Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality

        ' Create an EncoderParameters object.
        ' An EncoderParameters object has an array of EncoderParameter
        ' objects. In this case, there is only one
        ' EncoderParameter object in the array.
        Dim myEncoderParameters As New EncoderParameters(1)

        Dim myEncoderParameter As New EncoderParameter(myEncoder, 50&)
        myEncoderParameters.Param(0) = myEncoderParameter
        bmp1.Save("c:\TestPhotoQualityFifty.jpg", jgpEncoder, myEncoderParameters)

        myEncoderParameter = New EncoderParameter(myEncoder, 100&)
        myEncoderParameters.Param(0) = myEncoderParameter
        bmp1.Save("c:\TestPhotoQualityHundred.jpg", jgpEncoder, myEncoderParameters)

        ' Save the bitmap as a JPG file with zero quality level compression.
        myEncoderParameter = New EncoderParameter(myEncoder, 0&)
        myEncoderParameters.Param(0) = myEncoderParameter
        bmp1.Save("c:\TestPhotoQualityZero.jpg", jgpEncoder, myEncoderParameters)

    End Sub 'VaryQualityLevel



...


Private Function GetEncoder(ByVal format As ImageFormat) As ImageCodecInfo

    Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageDecoders()

    Dim codec As ImageCodecInfo
    For Each codec In codecs
        If codec.FormatID = format.Guid Then
            Return codec
        End If
    Next codec
    Return Nothing

End Function

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question