<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WinSoft.se &#187; JPG</title>
	<atom:link href="http://www.winsoft.se/tag/jpg/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.winsoft.se</link>
	<description>Development with focus on Visual Basic .NET</description>
	<lastBuildDate>Thu, 26 Jan 2012 19:28:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Loading and saving bitmap images</title>
		<link>http://www.winsoft.se/2008/12/loading-and-saving-bitmap-images/</link>
		<comments>http://www.winsoft.se/2008/12/loading-and-saving-bitmap-images/#comments</comments>
		<pubDate>Sat, 13 Dec 2008 10:59:05 +0000</pubDate>
		<dc:creator>Anders Hesselbom</dc:creator>
				<category><![CDATA[Visual Basic 8]]></category>
		<category><![CDATA[Bitmap]]></category>
		<category><![CDATA[JPG]]></category>

		<guid isPermaLink="false">http://www.winsoft.se/?p=25</guid>
		<description><![CDATA[Instances of the Bitmap class (System.Drawing.Bitmap) represent a bitmapped image. To load an image, just pass the filename to the Bitmap constructor. This example loads a jpeg image from the &#8220;my pictures&#8221; folder in the current user&#8217;s local storage. Dim MyPics As String = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) Dim B As New System.Drawing.Bitmap(MyPics &#38; &#8220;\duck.jpg&#8221;) The supported formats [...]]]></description>
			<content:encoded><![CDATA[<div>
<p>Instances of the Bitmap class (System.Drawing.Bitmap) represent a bitmapped image. To load an image, just pass the filename to the Bitmap constructor. This example loads a jpeg image from the &#8220;my pictures&#8221; folder in the current user&#8217;s local storage.</p>
<p><strong>Dim MyPics As String = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)<br />
Dim B As New System.Drawing.Bitmap(MyPics &amp; &#8220;\duck.jpg&#8221;)</strong></p>
<p>The supported formats are BMP, GIF, JPEG, PNG and TIFF.</p>
<p>To save the image, just call the function Bitmap.Save of the Bitmap instance, and specify the desired format by passing one of the read only properties in the ImageFormat class as the second argument. This code is saves the loaded image in the Bitmap instance in five different formats.</p>
<p><strong>B.Save(MyPics &amp; &#8220;\testsave.jpg&#8221;, Imaging.ImageFormat.Jpeg)<br />
B.Save(MyPics &amp; &#8220;\testsave.bmp&#8221;, Imaging.ImageFormat.Bmp)<br />
B.Save(MyPics &amp; &#8220;\testsave.gif&#8221;, Imaging.ImageFormat.Gif)<br />
B.Save(MyPics &amp; &#8220;\testsave.tif&#8221;, Imaging.ImageFormat.Tiff)<br />
B.Save(MyPics &amp; &#8220;\testsave.png&#8221;, Imaging.ImageFormat.Png)</strong></p>
<p>In some cases, you want to pass arguments to control compression and quality, for example when you save a JPEG image. The Save function has an overloaded version that can take codec parameters as an argument. To use that overload, I need a JPEG codec that can be found in the vector that is returned from the ImageCodecInfo.GetImageEncoders function. This is one piece of code that really would <a href="http://www.winsoft.se/2008/12/using-linq-to-locate-a-codec/" target="_self">look better if Linq is used</a>, but I do this in Visual Basic 8, so Linq is not available to me.</p>
<p><strong></strong></p>
<p><strong></strong></p>
<p><strong>Dim Codecs() As Imaging.ImageCodecInfo = Imaging.ImageCodecInfo.GetImageEncoders()<br />
For Each Codec As Imaging.ImageCodecInfo In Codecs<br />
If Codec.MimeType = &#8220;image/jpeg&#8221; Then<br />
&#8216;More code here<br />
Exit For<br />
End If<br />
Next</strong></p>
<p>At the remark &#8220;More code here&#8221;, I will now add code to actually save the image. This is the complete code for this example:</p>
<p><strong>Dim Codecs() As Imaging.ImageCodecInfo = Imaging.ImageCodecInfo.GetImageEncoders()<br />
For Each Codec As Imaging.ImageCodecInfo In Codecs<br />
If Codec.MimeType = &#8220;image/jpeg&#8221; Then<br />
&#8216;Create the desiered parameters in a list<br />
Dim HighCompression As New Imaging.EncoderParameter(Imaging.Encoder.Quality, 0)<br />
Dim AllMyParameters As New Imaging.EncoderParameters(1)<br />
AllMyParameters.Param(0) = HighCompression<br />
&#8216;Save the image using the codec and the parameter(s)<br />
B.Save(MyPics &amp; &#8220;\high_compression.jpg&#8221;, Codec, AllMyParameters)<br />
Exit For<br />
End If<br />
Next</strong></p>
<p>If you run this, you should end up with a picture of very poor quality. The compression (0) is the highest possible, and the file is as small as it can be.</p>
<p>The quality parameter can be a value from 0 to 100, so to get the best possible quality (lowest compression), change the quality parameter to 100, like this:</p>
<p><strong>Dim HighCompression As New Imaging.EncoderParameter(Imaging.Encoder.Quality, 100)</strong></p>
<p>This is the basics in loading and saving bitmap images in .NET 2.0.</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.winsoft.se/2008/12/loading-and-saving-bitmap-images/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

