resize image

Posted on 2007-04-03
Last Modified: 2010-04-23

I have an image with size of 60x100
Now I want to resize it, so it is 100x100, but not resize the image itself. The image itself should still be 60x100, but with 20 pix on the left and 20 pix on the right, this space should be transparent...

How can I do that?

Question by:Ruttensoft
  • 2
  • 2
LVL 85

Accepted Solution

Mike Tomlinson earned 500 total points
ID: 18844040
You can resize an image using code like this...

        Dim fileName As String = "c:\small.jpg"

        Dim fs As New System.IO.FileStream(fileName, IO.FileMode.Open)
        Dim img As Image = Image.FromStream(fs)

        Dim bmp As New Bitmap(100, 100)
        Dim g As Graphics = Graphics.FromImage(bmp)
        g.DrawImage(img, 20, 0)


        bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg)

But I'm not sure about the Transparent color part.  =\

Author Comment

ID: 18847173

Thanks for the hint

It's interesting, it did not work initially, the bitmap was bigger on the new drawn image.
I've now changed from filestream to direkt Image.Fromfile, and this is working
        Dim fileName As String = "c:\Portrait.png"
        Dim img As Bitmap = New Bitmap(Image.FromFile(fileName))
        Dim bmp As New Bitmap(100, 100)
        Dim g As Graphics = Graphics.FromImage(bmp)
        g.DrawImage(img, 20, 0)
        bmp.Save("c:\Portraita.png", System.Drawing.Imaging.ImageFormat.Png)

Then saving to png, and the transparent is also working... by jpg not...

But thanks for the hint


LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18847442
Ok...wonder why it was resizing the image in the original code?

Glad you figured out the rest though...  =)

Author Comment

ID: 18847451
Hm yeah I wonder too why that was not working... very strange... but yeah... it now works, thanks for your code, without I would not have figured it out...

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: (…
Internet Business Fax to Email Made Easy - With  eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now