<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://forums.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>System.Drawing/GDI+</title><link>http://forums.asp.net/150.aspx</link><description>Discuss manipulating and creating graphics using the System.Drawing namespace and GDI+.</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Re: retrieve image from database then resize it before rendering on webpage</title><link>http://forums.asp.net/thread/3256861.aspx</link><pubDate>Thu, 25 Jun 2009 05:51:45 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3256861</guid><dc:creator>olalekex</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3256861.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=150&amp;PostID=3256861</wfw:commentRss><description>&lt;p&gt;Hi Benno74,&lt;br /&gt;&lt;br /&gt;I made use of the tipps cmgray posted and it works&lt;br /&gt;&lt;br /&gt;the &amp;#39;outputSize&amp;#39; he mentioned is your own output size that you want the displayed image to have. for example you can replace the the line where &amp;#39;outputSize is with 100. but first you have to declare the outputsize in int&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // Create a bitmap from the stream&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Bitmap bmp = new Bitmap(stream);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Size new_size = new Size( );&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; int outputSize=100;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //resize based on the longer dimension&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (bmp.Width &amp;gt; bmp.Height) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new_size.Width = outputSize;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new_size.Height = (int)(((double)outputSize / (double)bmp.Width) * (double)bmp.Height);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new_size.Width = (int)(((double)outputSize / (double)bmp.Height) * (double)bmp.Width);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new_size.Height = outputSize;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;if this answer helps you please mark as answer&lt;/p&gt;&lt;p&gt;olalekex&lt;br /&gt;&lt;/p&gt;</description></item><item><title>Re: retrieve image from database then resize it before rendering on webpage</title><link>http://forums.asp.net/thread/2109367.aspx</link><pubDate>Tue, 15 Jan 2008 01:08:36 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2109367</guid><dc:creator>Benno74</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2109367.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=150&amp;PostID=2109367</wfw:commentRss><description>&lt;p&gt;Hi cmgray,&lt;/p&gt;&lt;p&gt;I&amp;#39;ve been looking at your code above for retrieving an image from a database and getting the image dimensions. You reference a variable called &amp;#39;outputSize&amp;#39; . I was wondering where the value for this comes from?&lt;/p&gt;&lt;p&gt;Basically, I am successfully retrieving the image from the database and adding it to the Response.OutputStream stream, but cannot seem to get the image dimensions from the data, so I was looking at your code as it looks like it will achieve this. Everything else about it looks fine but just a bit stuck on the &amp;#39;outputSize&amp;#39; value.&lt;/p&gt;&lt;p&gt;I know it was written a little while ago now, but any chance you - if you&amp;#39;re still around (or someone else) - could give me some guidance on this?&lt;/p&gt;&lt;p&gt;&lt;br /&gt;Thanks in advance,&lt;/p&gt;&lt;p&gt;Ben&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;</description></item><item><title>Re: retrieve image from database then resize it before rendering on webpage</title><link>http://forums.asp.net/thread/1209771.aspx</link><pubDate>Sat, 25 Feb 2006 18:14:22 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:1209771</guid><dc:creator>cmgray</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/1209771.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=150&amp;PostID=1209771</wfw:commentRss><description>&lt;p&gt;Here is some C# code that I use to do exactly that.&amp;nbsp; I pull the image from an image field in a database and resize it memory, then put it out to the output stream.&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;public partial class ShowImage : System.Web.UI.Page&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;SqlCommand photoCommand = new SqlCommand("SELECT Image FROM Photos WHERE PhotoID = @PhotoID", connection);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;photoCommand.Parameters.AddWithValue("@PhotoID", Request.QueryString["PhotoID"]);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;try {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;connection.Open( );&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Get bytes return from DB Command&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;byte[] b = (byte[])photoCommand.ExecuteScalar();&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(b.Length &amp;gt; 0)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Open a stream for the image and write the bytes into it&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;System.IO.MemoryStream stream = new System.IO.MemoryStream(b, true);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;stream.Write(b, 0, b.Length);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Create a bitmap from the stream&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Bitmap bmp = new Bitmap(stream);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Size new_size = new Size( );&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//resize based on the longer dimension&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (bmp.Width &amp;gt; bmp.Height) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;new_size.Width = outputSize;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;new_size.Height = (int)(((double)outputSize / (double)bmp.Width) * (double)bmp.Height);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;new_size.Width = (int)(((double)outputSize / (double)bmp.Height) * (double)bmp.Width);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;new_size.Height = outputSize;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Bitmap bitmap = new Bitmap(new_size.Width, new_size.Height, bmp.PixelFormat);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Graphics new_g = Graphics.FromImage(bitmap);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;new_g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;new_g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;new_g.DrawImage(bmp, -1, -1, bitmap.Width + 1, bitmap.Height + 1);&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;bmp.Dispose( );&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//Draw the bitmapt to the outputstream&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;bitmap.Dispose( );&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;new_g.Dispose( );&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Close the stream&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;stream.Close( );&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;connection.Close( );&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;catch (Exception exc) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;connection.Close( );&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;}&lt;br /&gt;&lt;/font&gt;&lt;/p&gt;</description></item><item><title>retrieve image from database then resize it before rendering on webpage</title><link>http://forums.asp.net/thread/1183307.aspx</link><pubDate>Mon, 30 Jan 2006 14:16:46 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:1183307</guid><dc:creator>kenpatt</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/1183307.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=150&amp;PostID=1183307</wfw:commentRss><description>&lt;p&gt;Here is my issue. I am trying to figure out the best way to pull an image from a database(the database holds the location of the image not the blob itself). Then while it is in memory resize the full sized image to a thumbnail. &lt;/p&gt;
&lt;p&gt;I have the query that I want to use which is:&lt;/p&gt;
&lt;p&gt;&amp;lt;%@ page explicit="true" language="vb" debug="true" %&amp;gt;&lt;br /&gt;&amp;lt;%@ import namespace="system.data" %&amp;gt;&lt;br /&gt;&amp;lt;%@ import namespace="MySql.Data.mysqlclient" %&amp;gt;&lt;/p&gt;
&lt;p&gt;Sub page_load &lt;/p&gt;
&lt;p&gt;&amp;nbsp;If not ispostback then&lt;/p&gt;
&lt;p&gt;'variables&lt;br /&gt;Dim myConn as MySql.Data.MySqlClient.MySqlConnection&lt;br /&gt;Dim sqlcmd as MySqlCommand&lt;br /&gt;Dim dtrhomes as MySqldatareader&lt;/p&gt;
&lt;p&gt;'database connection and query&lt;br /&gt;myconn = new MySql.Data.MySqlClient.MySqlConnection ("Server=myserver;Database=mydbase;Uid=xxxx;Pwd=xxxx;")&lt;br /&gt;myconn.open()&lt;br /&gt;sqlcmd = new MysqlCommand("SELECT propertypic FROM waters ORDER BY RAND() LIMIT 1",myconn)&lt;br /&gt;dtrhomes = sqlcmd.executereader()&lt;/p&gt;
&lt;p&gt;'bind repeater&lt;br /&gt;rpthomes.datasource = dtrhomes&lt;br /&gt;rpthomes.databind()&lt;/p&gt;
&lt;p&gt;dtrhomes.close()&lt;br /&gt;myconn.close()&lt;br /&gt;end if &lt;br /&gt;end sub&lt;br /&gt;&amp;lt;/script&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;asp:repeater ID="rpthomes" Runat="server"&amp;gt;&lt;br /&gt;&amp;lt;itemTemplate&amp;gt;&lt;br /&gt;&amp;lt;img src='&amp;lt;%# container.dataItem("propertypic") %&amp;gt;' &amp;gt;&lt;br /&gt;&amp;lt;/itemTemplate&amp;gt;&lt;br /&gt;&amp;lt;/asp:repeater&amp;gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;Now comes the hard part&amp;nbsp;how do I marry the above with this:&lt;/p&gt;
&lt;p&gt;&amp;lt;@ page content type="image/jpeg" %&amp;gt;&lt;br /&gt;&amp;lt;@ import namespace="system.drawing" %&amp;gt;&lt;br /&gt;&amp;lt;@ import namespace="system.drawing.imaging" %&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;script runat="server" &amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Sub page_load&lt;br /&gt;&amp;nbsp;Dim objBitmap As Bitmap&lt;br /&gt;&amp;nbsp;Dim objGraphics As Graphics&lt;/p&gt;
&lt;p&gt;'create bitmap&lt;br /&gt;objBitmap = new Bitmap(120,120)&lt;/p&gt;
&lt;p&gt;objGraphics = Graphics.FromImage( objBitmap )&lt;/p&gt;
&lt;p&gt;'display bitmap&lt;/p&gt;
&lt;p&gt;objBitmap.save( response.outputstram, imageformat.jpeg )&lt;/p&gt;
&lt;p&gt;End sub&lt;br /&gt;&amp;lt;/script&amp;gt;&lt;/p&gt;
&lt;p&gt;As you see I want to create an image that is 120x120 and that the image location is stored in the propertypic field in a database.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thanks for all the help&lt;/p&gt;
&lt;p&gt;Ken.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item></channel></rss>