views:

618

answers:

3

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!:)

+1  A: 

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

Saving Images to DB

griegs
how will i convert system.web.httppostedfilebase to system.drawing.bitmap?=)
tiff
unsure on that but i think the only way to resize is to load the image first. from your code it looks as though you're saving the location to the image in the database rather than the byte array. so you'll need to load the picture as a byte array first and then resize and then back to the file system.have you considered saving the file in an image field in the database? that way you can save the original, small and thumbnail versions of the file in a handy table.its easy in mvc to get the picture back and render to the view
griegs
your right about my saving directly to the database..i'm just new to mvc that's why..not really familiar with byte array.=)
tiff
Check out my edit to this post for instructions
griegs
If you need more then let me know and I'll provide.
griegs
okay, i'll try it..thanks.
tiff
i'm really having a hard time understanding codes, i'm just not that good in programming, but i'm trying though..=) i'm uploading the image as HttpPostedFileBase, how will i resize it if i can't convert it to bitmap or something and then back to HttpPostedFileBase for me to save it in my database?..hmmh,is my logic correct? please correct me if i'm wrong and if there's a more logical way, please do suggest..thank you!
tiff
Tiff, forget about saving to a database for the time being. I'll try and answer this in another answer rather than in a comment. stand by.
griegs
A: 

I have removed this answer in favour of the bigger, complete, version.

griegs
A: 

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();
        }
    }
}
griegs
hi Griegs..this post seems complicated for me to understand but better!=) I appreciate your effort in answering..I'll try to work my problem out with your latest post..thanks again!
tiff
If you need more help just add another comment to this and I'll try to help more. good luck
griegs