24. februar 2005 - 13:37Der er
13 kommentarer og 1 løsning
Update Image i databasen
Jeg har en MS SQL tabel med et Image felt. Jeg vil have en funktion der kan hente Image feltet ud, siden resize billedet til ratio af 640 og siden update Image feltet med det resize'ede billede.
hej curriculum - prøv at se om du kan få nedenstående til at funge med dit Image-felt. (errorhandling, tilpasning til din tabel og datalag mv. skal du nok lige kaste et blik på ;o)
mvh
using System; using System.IO; using System.Drawing; using System.Drawing.Imaging; using System.Data; using System.Data.SqlClient;
namespace ResizeDbImage { class Program { [STAThread] static void Main(string[] args) { int someImageId = 1; int sizeX = 200, sizeY = 400;
ImageDal dal = new ImageDal(); byte[] imagebytes = dal.GetImageBytes(someImageId);
// til ændring af størrelsen på en billede public static byte[] ResizeImage(byte[] image, int width, int height) { System.Drawing.Bitmap bmpOut = null;
Bitmap bmp = new Bitmap(new MemoryStream(image)); bmpOut = new Bitmap(width, height); Graphics g = Graphics.FromImage(bmpOut); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.FillRectangle(Brushes.White, 0, 0, width, height); g.DrawImage(bmp, 0, 0, width, height); bmp.Dispose();
MemoryStream ms = new MemoryStream(); bmpOut.Save(ms, ImageFormat.Jpeg); byte[] result = ms.ToArray(); ms.Close();
return result; } }
public class ImageDal { // du kan hente dine bytes sådan her : public byte[] GetImageBytes(int imageId) { SqlConnection connection = new SqlConnection("..."); SqlCommand command = new SqlCommand("select data from [image] where imageid = @pImageId", connection);
// eller et Image sådan her public Image GetImage(int imageId) { MemoryStream ms = new MemoryStream(GetImageBytes(imageId)); Image img = Image.FromStream(ms); ms.Close(); return img; }
// og du kan lave en opdatering sådan her : public void UpdateImage(int imageId, byte[] image) { SqlConnection connection = new SqlConnection("data source=JPPR-A193;initial catalog=ElmerPhoto;password=sa;persist security info=True;user id=sa;packet size=4096"); SqlCommand command = new SqlCommand("update [image] set data=@pImageData where ImageId=@pImageId", connection);
Nu får jeg en anden fejl... "Type 'Byte' has no constructors"
Dim image As Byte()
command.Connection.Open() Dim reader As SqlDataReader= command.ExecuteReader(CommandBehavior.CloseConnection) If (reader.Read()) Then image = New Byte(Convert.ToInt32(reader.GetBytes(0, 0, null, 0, Int32.MaxValue))) reader.GetBytes(0, 0, image, 0, image.Length) End If reader.Close()
tjah.... er som sagt ikke noget lys til vb, men det kan være det er noget i denne stil :
Dim image() As Byte
command.Connection.Open() Dim reader As SqlDataReader= command.ExecuteReader(CommandBehavior.CloseConnection) If (reader.Read()) Then Dim length as Int = ReDim image( Convert.ToInt32(reader.GetBytes(0, 0, null, 0, Int32.MaxValue)) ) image = reader.GetBytes(0, 0, image, 0, image.Length) End If reader.Close()
Declaring Array VariablesSee Also Arrays Overview | Array Usage | Advanced Features of Arrays | Arrays of Arrays | Collections as an Alternative to Arrays | Object-Oriented Programming in Visual Basic | Dim Statement | Byte Data Type | New | Short Data Type | Long Data Type | Preserve | Array.GetUpperBound Method | ReDim Statement | New Array variables are declared the same way as other variables, using the Dim statement. You follow the variable name with one or more pairs of parentheses to indicate that it is an array rather than a scalar (a variable containing a single value).
To declare a single-dimensional array variable
In your declaration, add one pair of parentheses after the variable name, as in the following example: Dim MySingleArray() As Integer To declare a multidimensional array variable
In your declaration, add one pair of parentheses after the variable name and place commas inside the parentheses to separate the dimensions, as in the following example: Dim My4DArray(,,,) As Short ' Four-dimensional array. To declare a jagged array variable
In your declaration, add as many pairs of parentheses after the variable name as there are levels of nested arrays, as in the following example: Dim MyJaggedArray()()() As Byte ' Array of arrays of Byte arrays. Initializing Arrays You can initialize an array variable as part of its declaration. You can do one of the following in the declaration statement:
Specify the initial length of one or more of the dimensions in the parentheses following the variable name, without assigning an array object to the variable. Assign an array object to the variable, using the New clause. When you use a New clause, you must follow it with braces ({}), even if they are empty. Assign an array object to the variable and supply initial lengths in the New clause. Assign an array object to the variable and supply initial element values in the New clause. You can supply both lengths and values in the same New clause. If you specify dimension lengths in the parentheses following the variable name, you must use a subsequent assignment statement to assign an array object to the variable. The following sample declarations show valid and invalid syntax for a single-dimensional array variable:
Dim BA(2) As Byte ' Currently contains Nothing (no object). Dim BA(2) As Byte = New Byte() ' INVALID (New after length specified). Dim BA() As Byte = New Byte() {} ' Empty Byte array object. Dim BA() As Byte = New Byte() ' INVALID (missing braces). Dim BA() As Byte = New Byte(2) ' INVALID (missing braces). Dim BA() As Byte = New Byte() {0,1,2} ' (0) through (2). Dim BA() As Byte = New Byte(2) {0,1,2} ' (0) through (2). You can initialize a multidimensional array variable in a similar manner. The following sample declarations show a two-dimensional array variable being declared as an Short array with 2 rows and 2 columns.
Dim S2X2(1, 1) As Short ' (0) through (1), (0) through (1). Dim S2X2(1, 1) As Short = New Short(,) ' INVALID (New after lengths). Dim S2X2(,) As Short = New Short(,) ' INVALID (missing braces). Dim S2X2(,) As Short = New Short(1, 1) ' INVALID (missing braces). Dim S2X2(,) As Short = New Short(,) {} ' Empty array object. Dim S2X2(,) As Short = New Short(1, 1) {} ' Elements have default value. Dim S2X2(,) As Short = New Short(,) {{5, 6}, {7, 8}} ' Four elements. The first argument represents the rows; the second argument represents the columns. In the arrays in the preceding declarations, the index values for both the row and the column range from 0 through 1.
When you initialize a jagged array variable, you can specify the dimension lengths only for the top-level array. The following sample declarations show valid and invalid syntax for an array of arrays of Byte elements:
Dim JB(1)() As Byte ' Array of two arrays of Byte elements. Dim JB(1)(1) As Byte ' INVALID (can only specify top-level size). Dim JB(1)() As Byte = New Byte()() ' INVALID (New after lengths). Dim JB()() As Byte = {New Byte() {}, New Byte() {}} ' Empty arrays. Dim JB()() As Byte = {New Byte(1) {}, New Byte(1) {}} ' Default values. Dim JB()() As Byte = {New Byte() {5, 6}, New Byte() {7, 8}} In the last of the preceding declarations, JB is initialized to two elements, JB(0) and JB(1), each of which is initialized to a two-element Byte array, the first with element values 5 and 6 and the second with elements 7 and 8.
Array Size Limits The length of every dimension of an array is limited to the maximum value of a Long data type, which is (2 ^ 64) - 1. The total size limit of an array varies, based on your operating system and how much memory is available. Using an array that exceeds the amount of RAM available on your system is slower because the data must be read from and written to disk.
Note Because arrays are objects in Visual Basic .NET, it is important to distinguish between an array object and an array variable. An array object, once created, does not change its size or rank. An array variable, however, can have a succession of different arrays assigned to it during its lifetime, and these can be of different sizes and ranks. Resizing Arrays You can resize an array at any time by assigning a different array object to the same array variable, using either ReDim or a standard assignment statement. The new array object can have different dimensions and even a different rank. This helps you manage memory efficiently. For example, you can use a large array for a short time and then ReDim it to a smaller size. This frees up memory you no longer need.
When you ReDim an array, its existing values are normally lost. However, you can retain them by including the Preserve keyword in the ReDim statement. For example, the following statement allocates a new array, initializes its elements from the corresponding elements of the existing MyArray, and assigns the new array to MyArray.
ReDim Preserve MyArray(10, 20) In a multidimensional array, you can change only the last dimension when you use Preserve. If you attempt to change any of the other dimensions, a run-time error occurs. If you do not know the current size of a dimension, you can use the GetUpperBound method, which returns the highest subscript value for the dimension you specify.
In the following example, the first line is valid, but the second line is not, because it attempts to change the first of two dimensions.
ReDim Preserve Matrix(Matrix.GetUpperBound(0), Matrix.GetUpperBound(1) + 10) ReDim Preserve Matrix(Matrix.GetUpperBound(0) + 10, Matrix.GetUpperBound(1)) See Also Arrays Overview | Array Usage | Advanced Features of Arrays | Arrays of Arrays | Collections as an Alternative to Arrays | Object-Oriented Programming in Visual Basic | Dim Statement | Byte Data Type | New | Short Data Type | Long Data Type | Preserve | Array.GetUpperBound Method | ReDim Statement | New
Sådan, nu fungerer det. 1000 tak for jhælpen snepnet. Sådan ser VB versionen ud, den er testet og fungerer.
Function GetPropertiesForDBImage(ByVal ImageId As Integer, ByVal sizeX As Integer, ByVal sizeY As Integer) Dim dal As ImageDal = New ImageDal Dim imagebytes As Byte() = dal.GetImageBytes(ImageId)
Dim TempWidth, TempHeight As Integer Dim ratio As Decimal TempWidth = sizeX TempHeight = sizeY
If TempWidth > 640 Or Tempheight > 640 Then If TempWidth > TempHeight Then ratio = (640/TempWidth) TempWidth = TempWidth*ratio TempHeight = TempHeight*ratio Else ratio = (640/TempHeight) TempWidth = TempWidth*ratio TempHeight = TempHeight*ratio End If End If
Dim img As Drawing.Image = FromStream(New MemoryStream(imagebytes)) Dim ms As New MemoryStream img.Save(ms, Imaging.ImageFormat.Jpeg)
dal.UpdateImage(ImageId, imagebytes, TempWidth, TempHeight) End Function
Public Function ResizeImage(ByVal image As Byte(), ByVal width As Integer, ByVal height As Integer) As Byte() Dim bmpOut AS System.Drawing.Bitmap
Dim bmp As Bitmap = New Bitmap(New MemoryStream(image)) bmpOut = New Bitmap(width, height)
Dim g As Graphics = Graphics.FromImage(bmpOut) g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic g.FillRectangle(Brushes.White, 0, 0, width, height) g.DrawImage(bmp, 0, 0, width, height) bmp.Dispose()
Dim ms As MemoryStream = New MemoryStream() bmpOut.Save(ms, ImageFormat.Jpeg)
Dim result As Byte() = ms.ToArray() ms.Close()
Return result End Function
Public Class ImageDal 'du kan hente dine bytes sådan her : Public Function GetImageBytes(ByVal imageId As Integer) As Byte() Dim connection As SqlConnection = new SqlConnection(ConfigurationSettings.AppSettings("Conn")) Dim command As SqlCommand = New SqlCommand("SELECT PiDbPicture FROM PiDb WHERE PiDbMainIdNo = @sImageId", connection)
command.Connection.Open() Dim reader As SqlDataReader = command.ExecuteReader(CommandBehavior.CloseConnection) If (reader.Read()) Then image = New Byte(Convert.ToInt64(reader.GetBytes(0, 0, image, 0, Int32.MaxValue))) {} reader.GetBytes(0, 0, image, 0, image.Length) End If reader.Close()
Return image End Function
'og du kan lave en opdatering sådan her : Public Sub UpdateImage(Byval ImageId As Integer, ByVal image As Byte(), ByVal iWidth As Integer, ByVal iHeight As Integer) Dim connection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("Conn")) Dim command As SqlCommand = New SqlCommand("UPDATE PiDb SET PiDbPicture = @pImageData, PiDbPictureWidth = @pImageWidth, PiDbPictureHeigth = @pImageHeight WHERE PiDbMainIdNo = @pImageId", connection)
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.