Reputation: 93
i want to resize an image before it is saved on my mysql database.. how will i process it? i have here the code for view, controller, and model.
View:
<form method="post" enctype="multipart/form-data" action="<%=url.action("PhotoInsert") %>">
<%Using Html.BeginForm()%>
<p>
<label for="despcription">Caption :</label>
<%=Html.TextArea("caption")%>
</p>
<p>
<label for="image">Image : </label>
<input type="file" id="image" name="image" />
</p>
<input type="submit" value="Insert" />
<%End Using%>
</form>
Controller:
Function PhotoInsert(ByVal caption As String, ByVal image As HttpPostedFileBase) As ActionResult
UploadDirectory = Path.GetDirectoryName(Request.PhysicalApplicationPath)
Dim fileName As String = Path.GetFileName(image.FileName)
Dim fullUploadpath As String = Path.Combine(UploadDirectory, fileName)
image.SaveAs(fullUploadpath)
dPhotos.pictureInsert(image:=image.FileName, caption:=caption)
End Function
Model:
Imports Microsoft.VisualBasic
Imports System.Data
Public Class ClassPhotosConnection
Inherits ClassConnection
Public Sub pictureInsert(ByVal image As String, ByVal caption As String)
Dim insert As String = String.Format("INSERT INTO pictures(Image, Caption) VALUES ('{0}','{1}')", image, caption)
UpdateData(insert)
End Sub
End Class
Thank you!:)
Upvotes: 1
Views: 2503
Reputation: 22770
I have removed this answer in favour of the bigger, complete, version.
Upvotes: 0
Reputation: 22770
Tiff, I quickly wrote this during my lunch break from various sites. It's in no way normalised and there are things that you can do better but given all the run-around you've had on this I thought giving you something that works would be good enough for now.
So here it is. I have run and tested it and it works no probs.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace ImageResize
{
class Program
{
private static byte[] ImageData;
private static byte[] SmallImageData;
private static Bitmap bmp;
static void Main(string[] args)
{
LoadImageIntoByteArray();
LoadByteArrayAsBitmap();
SaveFromStream();
}
private static void SaveFromStream()
{
using (Image img = Image.FromStream(new MemoryStream(SmallImageData)))
{
img.Save(@"flowers_thumb.jpg", ImageFormat.Jpeg);
}
}
private static void LoadByteArrayAsBitmap()
{
MemoryStream ms = new MemoryStream(ImageData);
bmp = new Bitmap(ms);
System.Drawing.Image oImg = System.Drawing.Image.FromStream(ms);
System.Drawing.Image oThumbNail = new Bitmap(100, 100);
Graphics oGraphic = Graphics.FromImage(oThumbNail);
oGraphic.CompositingQuality = CompositingQuality.HighQuality;
oGraphic.SmoothingMode = SmoothingMode.HighQuality;
oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle oRectangle = new Rectangle
(0, 0, 100, 100);
oGraphic.DrawImage(oImg, oRectangle);
MemoryStream ms2 = new MemoryStream();
oThumbNail.Save(ms2, ImageFormat.Jpeg);
ms2.Position = 0;
SmallImageData = new byte[ms2.Length];
ms2.Read(SmallImageData, 0, Convert.ToInt32(ms2.Length));
oGraphic.Dispose();
oImg.Dispose();
ms2.Close();
ms2.Dispose();
}
private static void LoadImageIntoByteArray()
{
FileStream fs = File.OpenRead(@"flowers.jpg");
ImageData = new byte[fs.Length];
fs.Read(ImageData, 0, ImageData.Length);
fs.Close();
}
}
}
Upvotes: 0
Reputation: 22770
You'll need to load the image and then use something like;
public Bitmap ResizeBitmap( Bitmap b, int nWidth, int nHeight )
{
Bitmap result = new Bitmap( nWidth, nHeight );
using( Graphics g = Graphics.FromImage( (Image) result ) )
g.DrawImage( b, 0, 0, nWidth, nHeight );
return result;
}
Untested but I used something like this in a previous project.
Ref site = Geek Noise
EDIT
Upvotes: 1