Goodmorning, I'm developing a cms, and my clients are gonna upload for sure image without respect the tips of dimensions so I would like to develop a resize image way to optimize the pages. I'm developing with VB, this is the first time that I use bitmap, and I'm not sure at all that I have choose the best solution that I need. I have understood that to resize the image I have before to transform it to a bitmap, then I can resize with my widht, height parameters and at the end, to show in my html tag img convert it to a 64bit string. Is this the best solution for me? And the point is that my picture loses quality, if I compare this one resized to an image "resized" with css with same dimension, it's totally different. I would like to maintain the same quality, thank you.
Here my resize.aspx page
<%@ Page Language="VB" Trace="false" Debug="true" aspcompat=true%>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Runtime.InteropServices" %>
<script runat="server">
Private Function CopyTo32BitArgb(image As System.Drawing.Image) As Bitmap
Dim imageCopy = New Bitmap(img, CInt(newWidth), CInt(maxHeight))
imageCopy.SetResolution(300, 300)
'if you want save and check Dpi imageCopy.Save("C:\inetpub\wwwroot\images\barber\prueba.jpg", ImageFormat.Jpeg)
For Each propItem As PropertyItem In image.PropertyItems
imageCopy.SetPropertyItem(propItem)
Next
Using g As Graphics = Graphics.FromImage(imageCopy)
g.InterpolationMode = InterpolationMode.HighQualityBicubic
g.DrawImage(image, New Rectangle(0, 0, 426, 284), New Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel)
g.Flush()
End Using
Return imageCopy
End Function
</script>
<%Dim original As System.Drawing.Image = System.Drawing.Image.FromFile("C:\inetpub\wwwroot\images\barber\1.jpeg", true)
Dim resized As System.Drawing.Image = CopyTo32BitArgb(original)
Dim memStream As MemoryStream = New MemoryStream()
resized.Save(memStream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim imgBytes as byte() = memStream.ToArray()
Dim imgString as string= Convert.ToBase64String(imgBytes)%>
<img style="display:block;" src="data:image/png;base64,<%=imgString%>"/>
<img style="height: 284px;" src="\images\barber\1.jpeg">