Solved

Control to bitmap

Posted on 2009-04-02
2
680 Views
Last Modified: 2013-12-17
i have a control that i am drawing to a bitmap. The problem that i am having is scaling the bitmap to a certain size. No matter what i have tried, after the control is drawn to the bitmap i cannot scale it down. When i view the bitmap in paint or window picture/fax viewer it isn't scaled, it is the original control drawn to bitmap size.

the factor variable is about .75 and is calculated in another method.

i have also tried the Graphics.DrawImage with the destination rectangle equally what it would be if the .75 scaling was applied in the scaletransform.  the drawimage does scale it down but it draws over the original control.drawtobitmap so i have both and the bitmap is still the same height.
Control s = (Control)control;
 
Bitmap bmp = new Bitmap((Image)new Bitmap(1, 1), control.Size);
 
s.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
 
Graphics g = Graphics.FromImage(bmp);
                        g.ScaleTransform(1F,(float)factor);
                       
                        g.SmoothingMode = SmoothingMode.HighQuality;
                        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                        g.CompositingQuality = CompositingQuality.HighQuality;
                        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
 
 bmp.Save(path + "myBitmap.Bmp", ImageFormat.Bmp);
                        g.Dispose();

Open in new window

0
Comment
Question by:p_davis
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 24050994
Try this please:
            Control s = (Control)control;
            Bitmap unscaledBMP = new Bitmap(control.Size.Width, control.Size.Height);
            s.DrawToBitmap(unscaledBMP, new Rectangle(0, 0, unscaledBMP.Width, unscaledBMP.Height));
 
            Bitmap scaledBMP = new Bitmap((int)((double)unscaledBMP.Width * factor), (int)((double)unscaledBMP.Height * factor));
            Graphics g = Graphics.FromImage(scaledBMP);
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(unscaledBMP, new Rectangle(0, 0, scaledBMP.Width, scaledBMP.Height));
            g.Dispose();
            scaledBMP.Save(path + "myBitmap.Bmp", ImageFormat.Bmp);

Open in new window

0
 
LVL 22

Author Comment

by:p_davis
ID: 24051168
awesome, that is something i can work with -- thanks  Idle Mind
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

773 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